#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int mhash(int x) { while (x%10 != x) { int t = 0; for (int i = x; i != 0; i/=10) t += i%10; x = t; } return x; } int main() { int k, n; cin >> k >> n; bool not_prime[n+1]; memset(not_prime, false, sizeof(not_prime)); not_prime[1] = true; for (int i = 2; i <= n; i++) { for (int j = i+i; j <= n; j+=i) { not_prime[j] = true; } } // for (int i = 0; i <= n; i++) cout << i << ": " << not_prime[i] << endl; vector memo; for (int i = k; i <= n; i++) if (!not_prime[i]) memo.push_back(i); // for (int i = 0; i < memo.size(); i++) cout << mhash(memo[i]) << endl; int ans, maxi = 0; bool used[10]; memset(used, false, sizeof(used)); used[mhash(memo[0])] = true; for (int l = 0, r = 1; r < memo.size(); r++) { if (used[mhash(memo[r])] || r == memo.size()-1) { if (maxi <= r-l) { maxi = r-l; ans = memo[l]; } while (mhash(memo[l]) != mhash(memo[r])) { used[mhash(memo[l])] = false; l++; } l++; if (l < memo.size() && maxi <= r-l+1) { maxi = r-l+1; ans = memo[l]; } } } if (!maxi) { cout << memo[memo.size()-1] << endl; }else { cout << ans << endl; } }