#include using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template using V = vector; template using VV = V>; template using VVV = V>; template using VVVV = VV>; #define rep(i,n) for(ll i=0ll;i void chmin(T& t, const U& u) { if (t > u) t = u; } template void chmax(T& t, const U& u) { if (t < u) t = u; } // cin.tie(nullptr); // ios::sync_with_stdio(false); // cout << fixed << setprecision(20); void solve(){ ll n,m; cin >> n >> m; V v(2001000, 1); v[0] = 0; v[1] = 0; REP(i,2,2001000) if(v[i]){ for(ll j=2*i;j<2001000;j+=i) v[j] = 0; v[i] = i; while(v[i] >= 10){ ll x = 0; while(v[i]){ x += v[i]%10; v[i] /= 10; } v[i] = x; } } ll ans = 0, len = 0; ll cnt = 0; ll R=n; V h(10, 0); REP(L,n,m+1){ while(R<=m){ if(v[R]==0) R++; else if(h[v[R]]) break; else{ h[v[R]]++; R++; cnt++; } } if(len<=cnt){ len = cnt; ans = L; } h[v[L]]--; if(v[L]>0) cnt--; } cout << ans << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int t=1; // cin >> t; rep(i,t) solve(); }