#include using namespace std; int N, K; vector prime; bool used[10]; int dp[10] = {0}; void Prime(){ int i, j, k; if(K <= 2) prime.push_back(2); for(i = 3; i <= 200000; i += 2){ k = 0; for(j = 3; j <= sqrt(i); j += 2){ if(i % j == 0){ k = 1; break; } } if(k == 0 && K <= i && i <= N) prime.push_back(i); } return; } int Hash(int num){ int res = 0; while (num > 0){ res += num % 10; num /= 10; } if (res >= 10) return Hash(res); return res; } int main(void){ cin >> K >> N; Prime(); for(int i = 0; i < prime.size(); ++i){ int j, h_num; for(j = 0; j < 10; ++j) used[j] = false; for(j = 0; i + j < prime.size() && !used[(h_num = Hash(prime[i + j]))]; ++j) used[h_num] = true; if(dp[j] < prime[i]) dp[j] = prime[i]; } for(int i = 9; i >= 0; --i){ if(dp[i] != 0){ cout << dp[i] << endl; break; } } return 0; }