#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define CLEAR(d) memset((d), 0, (sizeof((d)))) #define ALL(c) (c).begin(), (c).end() #define ABS(x) ((x < 0) ? -(x) : (x)) #define SORT(x) sort((x).begin(), (x).end()) #define RSORT(x) sort((x).begin(), (x).end(), greater() ) #define SIZE(a) ((int)((a).size())) #define MAX3(a, b, c) (max(max((a), (b)), (c))) #define MIN3(a, b, c) (min(min((a), (b)), (c))) #define MOD 1000000007 #define EPS 1e-5 #define PI (acos(-1)) #define INF 10000000 //=================================================== template inline std::string toString(T x) { std::ostringstream oss; oss << x; return oss.str(); } typedef pair PA; vector sieveOfEratosthenes(int n) { vector arr(n+1); for (int i = 0; i <= n; i++) arr[i] = 1; arr[0] = arr[1] = 0; for (int i = 2; i*i <= n; i++) if (arr[i]) for (int j = i * i; j <= n; j += i) arr[j] = 0; return arr; } int hashDigi(int num) { int hash = num; while (hash >= 10) { string s = toString(hash); hash = 0; REP(i, s.length()) hash += (int)(s[i] - '0'); } return hash; } int main() { int n, k; cin >> k >> n; vector arr = sieveOfEratosthenes(n); vector hash; for (int i = k; i <= n; i++) { if (arr[i]) hash.push_back(PA(hashDigi(i), i)); } int t[10], ans = -1, front = 0, len = 0, ma[20]; CLEAR(ma); CLEAR(t); REP(i, hash.size()) { if (t[hash[i].first]) { for (; hash[front].first != hash[i].first; front++) { t[hash[front].first] = 0; len--; } //t[hash[i].first] = 1; len++; front++; ma[len] = max(ma[len], hash[front].second); } else { t[hash[i].first] = 1; len++; ma[len] = max(ma[len], hash[front].second); } } for (int i = 15; i >= 1; i--) { if (ma[i] != 0) { ans = ma[i]; break; } } printf("%d\n", ans); return 0; }