#include using namespace std; #define int long long #define rep(i, a, b) for (int i=a;i<(b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; typedef vector vvi; constexpr int LIM = 1e6; constexpr int S = 1e3; array isPrime{}; vi eratot() { constexpr int R = LIM / 2; vi pr = {2}, sieve (S+1);pr.reserve((int)(LIM/log(LIM) * 1.1)); vector cp; for(int i = 3; i <= S; i+=2) if(!sieve[i]) { cp.push_back({i, i * i / 2}); for(int j = i*i; j <= S; j += 2 * i) sieve[j] = 1; } for(int L = 1 ; L <= R; L += S) { array block{}; for(auto &[p, idx]: cp) { for(int i =idx; i < S+L; idx = (i+=p)) block[i-L] = 1; rep(i, 0, min(S, R - L)) if(!block[i]) pr.push_back((L+i)*2 + 1); } } for(int i: pr) isPrime[i] = 1; return pr; } set get_next(int a) { setres; vector s; while(a > 0) { s.push_back('0' + (a % 10)); a = a / 10; } int number = 0; for(int idx = s.size() - 2; idx >= 0; idx--) { number = number * 10 + (s[idx] - '0'); } res.insert(number); number = 0; for(int idx = s.size() - 1; idx >= 1; idx--) { number = number * 10 + (s[idx] - '0'); } res.insert(number); return res; } vi primes; bool is_prime(int n) { if(n < 2) return false; if(n < LIM) return isPrime[n]; for(int p: primes) { if(n % p == 0) return false; } return true; } signed main() { cin.tie(0)->sync_with_stdio(0); primes = eratot(); int N; cin >> N; set curr; set next;next.insert(N); int m = 0; while(1) { curr.clear(); for(auto p: next) if(is_prime(p)) curr.insert(p); next.clear(); for(auto p: curr) { for(auto n: get_next(p)) { next.insert(n); } } if(curr.empty()) break; m++; } cout << m << endl; }