#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP(i,n) for(int i = 0; n > i; i++) using namespace std; typedef vector Ivec; typedef pair pii; int arr[200000]; void Eratosthenes(int N) { for (int i = 0; i < N; i++) { arr[i] = 1; } for (int i = 2; i < sqrt(N); i++) { if (arr[i]) { for (int j = 0; i * (j + 2) < N; j++) { arr[i *(j + 2)] = 0; } } } } int ketasum(int n, int base) {//桁の総数 345->3+4+5 if (n == 0)return 0; return ketasum(n - n%(base*10), base*10) + n % (base * 10) / base; } int main() { int a, b; scanf("%d %d", &a, &b); Eratosthenes(b+1); vector sosu, hash; for (int i = max(a, 2); b+1 >= i; i++) { if (arr[i]) { int n = i; while ((n / 10)) { n = ketasum(n, 1); } sosu.push_back(i); hash.push_back(n); } } int ans = 0, ansst; for (int i = sosu.size() - 1; 0 <= i; i--) { array al = {}; for (int j = i; 0 <= j; j--) { if (al[hash[j]]) { if (ans < i - j)ansst = sosu[j+1]; ans = max(ans, i-j); break; } else { al[hash[j]] = 1; } } } printf("%d\n", ansst); return 0; }