#include using namespace std; #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 = 1e4; constexpr int S = 1e2; bitset 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; } int pow(int base, int power) { int res = 1; while(power--) res *= base; return res; } set get_next(int a) { set res; vector s; while(a > 0) { s.push_back('0' + (a % 10)); a = a / 10; } for(int skip = 0; skip < s.size(); skip++) { int number = 0; for(int idx = s.size() - 1; idx >= 0; idx--) { if(idx == skip) continue; number = number * 10 + (s[idx] - '0'); } res.insert(number); } return res; } vi primes; bool is_prime(int n) { if(n < LIM) return isPrime[n]; for(int p: primes) { if(n % p == 0) return false; } return true; } int main() { cin.tie(0)->sync_with_stdio(0); primes = eratot(); int N; cin >> N; set curr; curr.insert(N); set next; int m = 0; while(!curr.empty()) { m++; for(auto p: curr) { for(auto n: get_next(p)) { if(is_prime(n)) { next.insert(n); } } } curr = next; next.clear(); } cout << m << endl; }