#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; #define MAX_PRIME 500000 bool isprime[MAX_PRIME+1]; vector primes; void init_prime(){ fill(isprime, isprime+MAX_PRIME+1, true); isprime[0] = isprime[1] = false; for(int i = 2; i <= MAX_PRIME; i++) { if (isprime[i]) { primes.push_back(i); for(int j = i*2; j <= MAX_PRIME; j += i) isprime[j] = false; } } } int myhash(int x){ if(x<10) return x; int ret = 0; while(x>0){ ret += x%10; x /= 10; } return myhash(ret); } int main(){ int K, N; cin >> K; cin >> N; init_prime(); vector hv, prm; rep(i,primes.size()){ if(K <= primes[i] && primes[i] <= N){ prm.push_back(primes[i]); hv.push_back(myhash(primes[i])); } } vector last(10, -1); int len = -1, ret = -1; rep(i,hv.size()){ rep(j,10){ last[j] = max(last[j], last[hv[i]]); } int maxlen = -1, p = -1; rep(j,10){ if(i-last[j]>maxlen){ maxlen = i-last[j]; p = last[j]; } } if(maxlen >= len){ len = maxlen; ret = prm[p+1]; } last[hv[i]] = i; } cout << ret << endl; return 0; }