#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; vector primes; void sieve(int n) { bool isPrime[200010]; for (int i = 0; i < 200010; i++)isPrime[i] = true; isPrime[0] = false; isPrime[1] = false; for (int i = 0; i <= n; i++) { if (isPrime[i] == true) { primes.push_back(i); for (int j = i * 2; j <= n; j += i) { isPrime[j] = false; } } } } int FrankHash(int n) { int tmp = 0; while (n > 0) { tmp += n % 10; n /= 10; if (tmp >= 10) { tmp = tmp % 10 + tmp / 10; } } return tmp; } int main() { int k, n; cin >> k >> n; sieve(n); vector v; vector hashed; for (int i = 0; i < primes.size(); i++) { if (primes[i] >= k && primes[i] <= n)v.push_back(primes[i]); } for (int i = 0; i < v.size(); i++) { hashed.push_back(FrankHash(v[i])); } map mp; for (int i = 1; i < 10; i++) { mp[i] = false; } int s = 0; int t = 0; int mx = 0; int mx_len = 0; while (1) { while (t < v.size() && !mp[hashed[t]]) { mp[hashed[t++]] = true; cerr << v[s] << " " << v[t] << endl; } if (s == v.size())break; if (mx_len < t - s) { mx_len = t - s; mx = v[s]; } else if (mx_len == t - s) { mx = max(mx, v[s]); } mp[hashed[s++]] = false; cerr << mx << " " << mx_len << endl; } cout << mx << endl; }