#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { ll N; cin >> N; ll ans = longINF; FOR(len, 1, 41) { FOR(m, 1, 10) { vector num(len, m); // p 進法での n auto make = [](ll p, ll n) -> vector { vector res; while (n > 0) { res.emplace_back(n % p); n /= p; } reverse(ALL(res)); return res; }; // p 進法で比較 a <= b auto compare = [](const vector a, const vector b) -> bool { if (a.size() != b.size()) return a.size() < b.size(); REP(i, a.size()) if (a[i] != b[i]) return a[i] < b[i]; return true; }; ll l = 1, r = max(3, N - 1); while (r - l > 1) { ll p = (l + r) >> 1; if (compare(make(p, N), num)) r = p; else l = p; } if (make(r, N) == num) chmin(ans, r); } } cout << ans << endl; }