#include #include #include #include #include #include #include using namespace std; #define FOR(i, s, e) for (int i = (s); i <= (e); i++) vector prime; int hash_[2000001]; int K; int N; int s = -1; int e = -1; int result = 0; int cou; int main() { cin >> K; cin >> N; for (int i = 2; i <= N; i++) { bool pri = true; for (int j = 0; j < prime.size() && pri; j++) { if (i % prime[j] == 0) pri = false; } if (pri) { prime.push_back(i); if (i >= K && s == -1) s = prime.size() - 1; if (i <= N) { e = prime.size() - 1; } } } FOR(i, 1, N) { if (i <= 9) hash_[i] = i; else { int tot = 0; int x = i; while (x != 0) { tot += x % 10; x /= 10; } hash_[i] = hash_[tot]; } } int start = e; bool used[10]; fill(used, used + 10, false); for (int i = e; i >= s; i--) { int h = hash_[prime[i]]; if (used[h] == false) { used[h] = true; } else { if (cou < start - i) { cou = start - i; result = prime[i + 1]; } i = start; start--; fill(used, used + 10, false); } } cout << result << endl; return 0; }