#include #include #include #include #include #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep(i, n) for (int i = (n)-1; i >= 0; i--) #define pb push_back #define mp make_pair #define fst first #define snd second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define pii pair #define vi vector using namespace std; template ostream& operator<<(ostream& o, const pair& p) { return o << "(" << p.first << "," << p.second << ")"; } template ostream& operator<<(ostream& o, const vector& vc) { o << "sz = " << vc.size() << endl << "["; for (const T& v : vc) o << v << ","; o << "]"; return o; } typedef long long ll; #define MAX 200001 vector prime; vector hs; bool check[MAX]; int N; int K; int mask[10]; bool can() { rep(i, 10) { if (mask[i] >= 2) { return false; } } return true; } ll ok(int length) { for (int i = 0; i < 10; i++) { mask[i] = 0; } if (hs.size() < length) { return -1; } for (int i = 1; i <= length; i++) { mask[hs[hs.size() - i]]++; } if (can()) { return prime[hs.size() - length]; } int head = (int)(hs.size() - length) - 1; int tail = hs.size() - 1; for (; head >= 0; head--, tail--) { mask[hs[tail]]--; mask[hs[head]]++; if (can()) { return prime[head]; } } return -1; } int main() { cin >> K >> N; rep(i, N + 1) { check[i] = true; } check[0] = check[1] = false; for (ll i = 2; i <= N; i++) { if (check[i]) { for (ll j = 2; i * j <= N; j++) { check[i * j] = false; } } } for (ll i = K; i <= N; i++) { if (check[i]) { prime.pb(i); hs.pb(i % 9); } } ll ans; for (int l = 7; l >= 1; l--) { if (ok(l) != -1) { ans = ok(l); break; } } cout << ans << endl; return 0; }