#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define int ll using namespace std; #define REP(i,a,b) for(int i=a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) typedef long long ll; bool pr[200001]; int PSIZE; int vpr[200001]; int phash[200001]; int memo[200001]; int get_hash(int x) { if(memo[x] > 0) { return memo[x]; } if(x < 10) { return x; } string s = to_string(x); int sum = 0; int size = s.size(); rep(i, size) { sum += s[i]-'0'; } return memo[x] = get_hash(sum); } signed main() { int K, N; cin >> K >> N; fill(pr, pr+200001, true); pr[0] = pr[1] = 0; rep(i, N+1) { if(pr[i]) { pr[i] = 1; if(K <= i) { vpr[PSIZE++] = i; } for(int j=i*i; j<=N+1; j+=i) { pr[j] = 0; } } } if(PSIZE == 0) { cout << 0 << endl; return 0; } rep(i, PSIZE) { phash[i] = get_hash(vpr[i]); } int maxlen = 0, ans = 0; int l = 0, r = 0; unordered_set st; // deque dq; while(1) { if(r >= PSIZE) { break; } if(!st.count(phash[r])) { st.insert(phash[r]);// dq.push_back(vpr[r]); if(maxlen <= (int)st.size()) { maxlen = st.size(); ans = vpr[l]; /* for(auto const& ii: dq) { cout << ii << ' '; } cout << endl; */ } r++; } else { while(1) { st.erase(phash[l++]); if(l == r) { break; } if(!st.count(phash[r])) { break; } } } } cout << ans << endl; return 0; }