#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define REPR(i, n) for(ll i = n;i >= 0;i--) #define FOR(i, m, n) for(ll i = m;i < n;i++) #define FORR(i, m, n) for(ll i = m;i >= n;i--) #define REPO(i, n) for(ll i = 1;i <= n;i++) #define ll long long #define INF (ll)1 << 60 #define MINF (-1 * INF) #define ALL(n) n.begin(),n.end() #define MOD 1000000007 #define P pair ll a, b, ma = 0, ans; vector prime, h; bool IsPrime(ll n) { if (n <= 1)return false; for (ll i = 2; i <= sqrt(n); i++) { if (n % i == 0)return false; } return true; } int hs(ll a) { ll res = a; while (res > 9) { ll next = 0; while (res > 0) { next += res % 10; res /= 10; } res = next; } return res; } int main() { cin >> a >> b; FOR(i, a, b + 1)if (IsPrime(i))prime.push_back(i); REP(i, prime.size())h.push_back(hs(prime[i])); REPR(i, h.size() - 1) { ll now = 0; set se; FOR(j, i, h.size()) { if (se.find(h[j]) != se.end())break; else { se.insert(h[j]); now++; } } if (ma < now) { ma = now; ans = prime[i]; } } cout << ans << endl; }