#include #define rep(i, a) for (int i = 0; i < (a); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) for (int i = (a) - 1; i >= 0; i--) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) using namespace std; typedef long long ll; const ll inf = 1e9; const ll mod = 1e9 + 7; int K, N; const int maxn = 1e6; bool isprime[maxn]; vector prime; vector h; int f(int n) { return 1 + (n - 1) % 9; } void sieve() { fill(isprime, isprime + maxn, true); for (ll i = 2; i < maxn; i++) { if (isprime[i]) { prime.push_back(i); h.push_back(f(i)); for (ll j = i * i; j < maxn; j += i) { isprime[j] = false; } } } } int main() { sieve(); int K, N; cin >> K >> N; K = lower_bound(prime.begin(), prime.end(), K) - prime.begin(); N = upper_bound(prime.begin(), prime.end(), N) - prime.begin(); int r = K; set s; pair ans(-1, 0); rep2 (i, K, N) { while (r < N && !s.count(h[r])) { s.insert(h[r]); r++; } ans = max(ans, make_pair((int)s.size(), prime[i])); s.erase(h[i]); } cout << ans.second << endl; return 0; }