#include #define int long long #define REP(i, b) for(int i = 0; i < (b); i++) #define REPS(i, b) for(int i = 1; i <= (b); i++) #define ALL(v) (v).begin(), (v).end() using namespace std; using pi = pair; using vi = vector; using vs = vector; using vb = vector; using vpi = vector; using vvi = vector; using vvb = vector; const int INF = 1e10; const int MOD = 1e9+7; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int h(int n) { int res = 0; while(n > 0) { res += n % 10; n /= 10; } if(res >= 10) return h(res); return res; } signed main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); const int MAX = 200000; int K, N; cin >> K >> N; vb prime(MAX+1, true); prime[0] = prime[1] = false; for(int i = 2; i * i <= MAX; i++) { if(prime[i]) { for(int j = 2*i; j <= MAX; j += i) prime[j] = false; } } vpi table; for(int i = K; i <= N; i++) if(prime[i]) table.push_back(make_pair(i, h(i))); int len = 0, x = table[0].first; int l = 0, r = 0; vi m(10, 0); for(int l = 0; l < table.size(); l++) { while(r < N && m[table[r].second] == 0) { m[table[r].second]++; r++; } if(chmax(len, r-l)) x = table[l].first; m[table[l].second]--; } cout << x << endl; }