#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vs = vector; using pii = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; using vpdd = vector; const int inf = (1 << 30) - 1; const ll INF = 1LL << 60; //const int MOD = 1000000007; const int MOD = 998244353; vector Eratosthenes(int N) { vector isPrime(N + 1, true); isPrime[0] = isPrime[1] = false; for (int p = 2; p * p <= N; ++p) { if (!isPrime[p]) continue; for (int q = p * 2; q <= N; q += p) { isPrime[q] = false; } } return isPrime; } ll sumDigits(ll x) { int ret = 0; do { ret += x % 10; x /= 10; } while (x > 0); return ret; } int f(int x) { if (x < 10) return x; return f(sumDigits(x)); } int main() { int k, n; cin >> k >> n; vb isPrime = Eratosthenes(n); vpii p; for (int i = k; i <= n; i++) { if (isPrime[i]) { p.push_back({ i, f(i) }); } } int ans = 0; int maxsize = 0; set st; int r = 0; for (int l = 0; l < p.size(); l++) { while (r < p.size() && st.find(p[r].second) == st.end()) { st.insert(p[r].second); r++; } if (st.size() >= maxsize) { ans = p[l].first; maxsize = st.size(); } if (l == r) r++; else st.erase(p[l].second); } cout << ans << endl; return 0; }