#include using namespace std; #define fi first #define se second #define pb push_back using vi = vector ; using ll = long long; using pii = pair ; //~ const ll mod = 998244353; const ll mod = 1e9 + 7; ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a; for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; } bool check(ll n, ll p) { if(p == 0 || p == 1) return 0; vector v; while(n) { v.pb(n % p); n /= p; } sort(v.begin(), v.end()); return (v[0] == v.back()); } int main() { ios :: sync_with_stdio(false); ll n; cin >> n; ll ans = n; for(ll i = 2; i <= ll(1e6); i ++) if(check(n, i)) ans = min(ans, i); for(ll i = 1; i * i <= n; i ++) if(n % i == 0) { for(ll x : {i - 1, n / i - 1}) if(check(n, x)) ans = min(ans, x); } cout << ans << '\n'; }