#include #include #include using namespace std; #define N_MAX 210000 int K, N; bool isnotprime[N_MAX + 1]; vector> mask; void make_prime() { isnotprime[0] = isnotprime[1] = 1; for (int i = 2; i * i <= N_MAX; i++) { if (!isnotprime[i]) { for (int j = i * i; j <= N_MAX; j += i) { isnotprime[j] = 1; } } } } void make_mask() { for (int i = 2; i <= N_MAX; i++) { if (!isnotprime[i]) { int x = i; while (9 < x) { int tmp = x; x = 0; while (tmp) { x += tmp % 10; tmp /= 10; } } mask.emplace_back(i, 1 << x - 1); } } } int main() { make_prime(); make_mask(); cin >> K >> N; int len_max = 0, res = 0; auto it = lower_bound(mask.begin(), mask.end(), make_pair(K, 0)); while (it != mask.end()) { if (N < it->first) break; int bit = 0, len = 0; for (auto jt = it; jt != mask.end(); jt++) { if (bit & jt->second || N < jt->first) { if (len_max <= len) { len_max = len; res = it->first; } break; } bit |= jt->second; len++; } it++; } cout << res << '\n'; return 0; }