#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) { string s = to_string(n); while (s.length() > 1) { int tmp = 0; for (int i = 0; i < s.length(); i++) { tmp += s[i] - '0'; } s = to_string(tmp); } return s[0] - '0'; } 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++) { cerr << FrankHash(v[i]) << endl; 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; mp[hashed[s]] = true; while (1) { while (t + 1 < v.size() && mp[hashed[t + 1]] == false) { t++; mp[hashed[t]] = true; cerr << v[s] << " " << v[t] << endl; } if (mx_len < t - s + 1) { mx_len = t - s + 1; mx = v[s]; } else if (mx_len == t - s + 1) { mx = max(mx, v[s]); } mp[hashed[s]] = false; s++; if (v.size() - s < mx_len)break; cerr << v[s] << " " << v[t] << " " << mx << " " << mx_len << endl << endl; } cout << mx << endl; }