#include #define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i)) #define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair pii; typedef pair pll; template bool chmax(T &a, const T &b){if(a bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;} template T div_floor(T a, T b){ if(b < 0) a *= -1, b *= -1; return a>=0 ? a/b : (a+1)/b-1; } template T div_ceil(T a, T b){ if(b < 0) a *= -1, b *= -1; return a>0 ? (a-1)/b+1 : a/b; } constexpr lint mod = 1e9+7; constexpr lint INF = mod * mod; constexpr int MAX = 200010; vector min_factor, prime; void sieve(int n){ min_factor.resize(n+1, 0); For(i, 2, n+1){ if(min_factor[i] == 0){ min_factor[i] = i; prime.push_back(i); } for(int x: prime){ if(x*i > n || x > i) break; min_factor[x*i] = x; } } } vector prime_f(int n){ vector ret; while(n > 1){ if(ret.empty() || ret.rbegin()->fi != min_factor[n]){ ret.emplace_back(min_factor[n], 1); } else ++ret.rbegin()->se; n /= min_factor[n]; } return ret; } bool is_prime(int n){ return min_factor[n] == n; } int dp[MAX]; int f(int n){ if(dp[n]) return dp[n]; if(0<=n && n<10) return dp[n] = n; int ret = 0; while(n){ ret += n % 10; n /= 10; } return f(ret); } int main(){ int K, n; scanf("%d%d", &K, &n); sieve(n); vector p, h; For(i, K, n+1)if(is_prime(i)){ p.push_back(i); h.push_back(f(i)); } int ans, len = 0; for(int l=0; l S; while(r < p.size() && S.find(h[r]) == S.end()){ S.insert(h[r]); ++r; } if(len <= r-l){ len = r-l; ans = p[l]; } S.erase(h[l]); ++l; } printf("%d\n", ans); }