#include #include #include #include using namespace std; typedef __int128 Int; Int mulMod(Int x, Int k, Int m) { if (k == 0) return 0; if (k % 2 == 0) return mulMod((x+x) % m, k/2, m); else return (x+mulMod(x, k-1, m)) % m; } Int powMod(Int x, Int k, Int m) { if (k == 0) return 1; if (k % 2 == 0) return powMod(mulMod(x, x, m) % m, k/2, m); else return mulMod(x, powMod(x, k-1, m), m); } bool suspect(Int a, int s, Int d, Int n) { Int x = powMod(a, d, n); if (x == 1) return true; for (int r = 0; r < s; ++r) { if (x == n - 1) return true; x = mulMod(x, x, n); } return false; } bool isPrime(Int n) { if (n <= 1 || (n > 2 && n % 2 == 0)) return false; int test[] = {2,3,5,7,11,13,17,19,23,27,29,31,-1}; Int d = n - 1, s = 0; while (d % 2 == 0) ++s, d /= 2; for (int i = 0; test[i] < n && test[i] != -1; ++i) if (!suspect(test[i], s, d, n)) return false; return true; } vector eratosthenes_sieve(int n){ vector res(n+1); if(n<2) return res; res[2]=1; for(int i=3;i<=n;i+=2){ if(i*i<=n && !res[i]) for(int j=i+i+i;j<=n;j+=i+i) res[j]=1; res[i]=!res[i]; } return res; } string s; Int n; vector p; int solve_naive(Int n){ for(int i=3;;i++){ vector ok(2*n); ok[0] = 1; for(int _=9;_--;)for(int j=0;j=i) ok[j-i] = 1; if(j%i) ok[j-1] = 1; ok[j+i] = 1; } if(ok[n-1]){ return i; } } } int solve(Int n){ for(int i=3;;i++){ set visited({1}), col({1}), q({1}); int f = 0; while(!q.empty() && (col.size() < i || i % 2)){ int j = *q.begin(); q.erase(q.begin()); if(j == n){ f = 1; break; } vector d; if(j%i != 0) d.push_back(j+1); if(i < j) d.push_back(j-i); if(j%i != 1) d.push_back(j-1); d.push_back(j+i); for(int v : d) if(v <= n && !visited.count(v) && !p[v]){ visited.insert(v); col.insert(v%i); q.insert(v); } } if(!f && col.size() == i && i % 2 == 0){ f = (n % i != 1) || !isPrime(n - i); } if(f){ // if(i != solve_naive(n)){ // cout << (int)n << ": " << i << ", " << solve_naive(n) << endl; // exit(0); // } return i; } } } int main(int argc, char *argv[]){ p = eratosthenes_sieve(100000); /* for(int i=4;;i++)if(!p[i]){ solve(i); } */ cin >> s; for(char c : s) n = n*10 + c - '0'; cout << solve(n) << endl; return 0; }