#include using namespace std; using pint = pair; #define REP(i, n) for (long long i = 0; i < (long long)(n); ++i) #define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i) int main() { long long N; cin >> N; auto solve = [&]() -> long long { for (long long p = 2; p * p <= N; ++p) { vector digit; long long N2 = N; while (N2) { digit.push_back(N2 % p); N2 /= p; } sort(digit.begin(), digit.end()); if (digit[0] == digit.back()) return p; } long long res = max(N-1, 2LL); for (long long a = 1; a * a <= N; ++a) { if (N % a != 0) continue; long long p = N / a - 1; if (a >= p) continue; res = min(res, p); } return res; }; cout << solve() << endl; }