#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define INF 1000000001 #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a); i >= (b); i--) using namespace std; typedef long long ll; typedef pair pii; const double PI = acos(-1.0); int N, K; vector primes; void getprimes(int k, int n) { vector v(n+1); int d=2; FOR(d,2,n+1) { if (v[d]) continue; if (d >= k && d <= n) primes.push_back(d); int tmp=d*2; while (tmp <= n) { v[tmp] = true; tmp += d; } } } int gethashval(int p) { while (p >= 10) { int x=0, q = p; while (q) { x += q % 10; q /= 10; } p = x; } return p; } bool collision(vector &used) { FOR(i,1,10) { if (used[i]>=2) return true; } return false; } int main() { ios::sync_with_stdio(false); cin >> K >> N; getprimes(K, N); vector hash(primes.size()); FOR(i,0,primes.size()) { hash[i] = gethashval(primes[i]); } vector used(10, 0); int ans=0, maxlen=0, l=0, r=0, hsz=hash.size(); while (l < hsz) { if (r == hsz) { used[hash[l++]]--; } else if (!collision(used)) { used[hash[r++]]++; } else { used[hash[l++]]--; } if (!collision(used)) { if (maxlen <= r-l) { maxlen = r-l; ans = primes[l]; } } } cout << ans << endl; return 0; }