#include using namespace std; typedef long long ll; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++) #define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++) #define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--) #define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((ll)(x).size()) #define len(x) ((ll)(x).length()) #define endl "\n" struct isPrime { private: vector data; public: isPrime() {} isPrime(int n) { data.resize(n + 1, true); data[0] = data[1] = false; for(long long i = 2; (i * i) <= n; i++) { if (!data[i]) continue; for(long long j = 2; (i * j) <= n; j++) { data[i * j] = false; } } } bool is_prime(int n) { return data[n]; } vector get_primes_vector() { vector ans; int n = data.size(); for(int i = 0; i < n; i++) { if (data[i]) ans.push_back(i); } return ans; } set get_primes_set() { set ans; int n = data.size(); for(int i = 0; i < n; i++) { if (data[i]) ans.insert(i); } return ans; } }; ll get_hash(ll n) { while(n >= 10) { ll tmp = 0; while(n > 0) { tmp += n % 10; n /= 10; } n = tmp; } return n; } int main() { cin.tie(0); ios::sync_with_stdio(false); // ifstream in("input.txt"); // cin.rdbuf(in.rdbuf()); ll k, n; cin >> k >> n; isPrime ip(n); auto pv = ip.get_primes_vector(); auto it = lower_bound(all(pv), k); ll l = (it - pv.begin()) - 1; ll r = l; set s; ll mcnt = 1, mval = pv[l]; while(l < sz(pv)) { if (l >= 0) { s.erase(get_hash(pv[l])); } l++; while((r + 1) < sz(pv)) { ll v = get_hash(pv[r + 1]); if (s.count(v) > 0) break; s.insert(v); r++; } if ((r - l) >= mcnt) { mcnt = r - l; mval = pv[l]; } } cout << mval << endl; return 0; }