#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,n) for(int (i) = 0;(i) < (n) ; ++(i)) #define REPS(a,i,n) for(int (i) = (a) ; (i) < (n) ; ++(i)) #if defined(_MSC_VER)||__cplusplus > 199711L #define AUTO(r,v) auto r = (v) #else #define AUTO(r,v) typeof(v) r = (v) #endif #define ALL(c) (c).begin() , (c).end() #define EACH(it,c) for(AUTO(it,(c).begin());it != (c).end();++it) #define LL long long #define int LL #define INF 99999999 #define DIV 1000000007 #define QUICK_CIN ios::sync_with_stdio(false); cin.tie(0); #define InitArr1(c,n) memset(&c[0],0,sizeof(int)*n) #define InitArr2(c,n) memset(&c[0][0],0,sizeof(int)*n) template bool valid(T x, T w) { return 0 <= x&&x < w; } int di[4] = { 1, -1, 0, 0 }; int dj[4] = { 0, 0, 1, -1 }; using namespace std; //----------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------- int k, n; signed main() { QUICK_CIN; while (cin >> k >> n) { bool prime[210000]; REP(i, 210000) prime[i] = false; for (int i = 2; i*i <= 210000; ++i) { for (int j = i * 2; j < 210000; j += i) { prime[j] = true; } } vector primes(0); vector val(0); REP(i, 209998) { if (!prime[i + 2]) primes.push_back(i + 2); } for (auto x : primes) { int s = 0; int z = 0; do { while (x) { s += x % 10; x /= 10; } x = s; z = s; s = 0; } while (z > 9); val.push_back(z); } bool buf[10]; REP(i, 10) { buf[i] = false; } int si(-1), gi(-1); while (k > primes[++si]); while (n >= primes[++gi]); gi--; int max_i = si; int max_v = 0; int ii = si; int jj = si; while (jj <= gi) { while (!buf[val[jj]] && jj <= gi) { buf[val[jj]] = true; ++jj; if (jj-ii >= max_v) { max_v = jj-ii; max_i = ii; } } while (buf[val[jj]]) { buf[val[ii]] = false; ++ii; } } cout << primes[max_i] << endl; } }