#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define REP(i,n) for(ll i=0; i<(n); ++i) #define TEN(x) ((ll)1e##x) #define ALL(v) (v).begin(), (v).end() // [a,b)の整数に対して篩 O((b-a) log log b): is_prime[i-a] = true ⇔ iが素数 vector segment_sieve(ll a, ll b){ vector t_small(sqrt(b)+1, true); vector t(b-a, true); if (0 - a >= 0) t[0 - a] = false; if (1 - a >= 0) t[1 - a] = false; for(ll i = 2; i*i < b; ++i) if(t_small[i]){ for(ll j = 2*i; (ll)j*j < b; j+=i) t_small[j] = false; // [2, √b)の篩 for(ll j = max(2LL, (a+i-1)/i) * i; j < b; j+=i) t[j - a] = false; // [a,b)の篩 } return t; } // 数字根 // 345 -> 3+4+5 = 12 -> 1+2 = 3 ≡ 345 (mod 9) ll numroot(ll a){ if(a!=0 && a%9==0) return 9; return a%9; } int main(){ ll k, n; cin >> k >> n; auto t = segment_sieve(k, n + 1); vector v; v.reserve(t.size()); REP(i, t.size()) if (t[i]) v.push_back(i + k); vector r(v.size()); REP(i, v.size()) r[i] = numroot(v[i]); vector used(10, false); vector max_l(11, -1); ll end = v.size(); for (ll i = v.size() - 1; i >= 0; --i) { while (used[r[i]]) { end--; used[r[end]] = false; } used[r[i]] = true; if (max_l[end - i] == -1) max_l[end - i] = v[i]; } reverse(ALL(max_l)); for (auto x : max_l) if (x != -1) { cout << x << endl; return 0; } }