#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; bool ok(ll n, ll m){ ll tmp = m; set st; while(n){ st.insert(n%m); n /= m; } return st.size() == 1; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n; cin >> n; ll ans = 1e15; for(ll m = 2; m*m <= n; m++){ if(ok(n, m)) chmin(ans, m); } for(ll k = 1; k*k <= n; k++){ if(n%k != 0) continue; ll m = (n/k)-1; if(k < m) chmin(ans, m); } cout << ans << endl; }