#include using namespace std; using ll = long long; using ull = unsigned long long; #define cerr if(0) cout map bc; bool is_prime(ll k) { if(k < 2) return false; if(k == 2) return true; if(k % 2 == 0) return false; for(ll i = 3; i*i <= k; i += 2) { if(k % i == 0) return false; } return true; } int nom(ll k) { cerr << "k: " << k << '\n'; if(bc.contains(k)) return bc[k]; if(!is_prime(k)) { bc[k] = 0; return 0; } int bst = 1; for(ll b = 1; b < 10*k; b*= 10) { ll tr = k % b; ll hd = (k / (b*10)); ll nw = tr + (hd * b); if(nw == k) break; if(is_prime(nw)) { } bst = max(bst, 1 + nom(nw)); } bc[k] = bst; cerr << "k: " << k << '\n'; cerr << bst << '\n'; return bst; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; cout << nom(n) << '\n'; return 0; }