#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define endl '\n' #define all(v) (v).begin(), (v).end() #define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end()) typedef long long ll; typedef pair P; typedef unsigned int uint; const int inf = 1000000009; bool is_prime[200010]; vector sieve(int k, int n) { vector res; memset(is_prime, true, sizeof(is_prime)); is_prime[0] = is_prime[1] = false; for (int i = 2; i <= n; i++) { if (is_prime[i]) { if (i >= k) res.push_back(i); for (int j = i + i; j <= n; j += i) is_prime[j] = false; } } return res; } int h(int n) { if (n < 10) return n; int k = 0; while (n > 0) { k += (n % 10); n /= 10; } return h(k); } int solve(int k, int n) { vector ps = sieve(k, n); vector ori = ps; int len = ps.size(); for (int i = 0; i < len; i++) ps[i] = h(ps[i]); int res = ori[0], s = 1, maxi = 1; bool used[10]; memset(used, false, sizeof(used)); used[ps[0]] = true; int l, r; l = 0; r = 1; while (r < len) { if (!used[ps[r]]) { s++; used[ps[r]] = true; if (s >= maxi) { maxi = s; res = ori[l]; } } else { while (ps[l] != ps[r]) { s--; used[ps[l]] = false; l++; } l++; if (s == maxi) { res = ori[l]; } } r++; } return res; } int main() { int k, n; scanf("%d%d", &k, &n); printf("%d\n", solve(k, n)); }