#include #include #include #include #include #include #include #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) using namespace std; typedef long long int lli; lli MOD = 1000000007; vector prime(int n, int k) { bool isprime[2000005] = {}; vector primes; for (int i = 2; i <= n; i++) { if (!isprime[i]) { if (i >= k) primes.push_back(i); for (int j = 2 * i; j <= n; j += i) { isprime[j] = true; } } } return primes; } int memo[200005] = {}; int h(int g) { if (memo[g]) return memo[g]; int sum = 0; while (g > 0) { sum += g % 10; g /= 10; } if (sum < 10) return memo[g] = sum; else return memo[g] = h(g); } int main() { int k, n; cin >> k >> n; vector p = prime(n, k); int ans = 0; int aa = 0; for (int i = 0; i < p.size(); i++) { set u; for (int j = i + 1; j < p.size(); j++) { if (u.find(h(p[j])) != u.end()) { if (ans < j - i) { ans = max(ans, j - i); aa = p[i]; } break; } else { u.insert(h(p[j])); } } } cout << aa << endl; }