import std.stdio; import std.string; import std.array; import std.typecons; import std.typetuple; import std.container; import std.algorithm; import std.conv; import std.math; import std.format; alias TypeTuple tie; void readlnToken(T...)(auto ref T args) { import std.stdio; import std.conv; import std.string; import std.array; auto line = split(readln().strip); foreach(ref arg; args) { arg = to!(typeof(arg))(line[0]); line = line[1..$]; } assert(line.empty()); // got all token?? } auto sieve(int n) { auto is_prime = new bool[](n+1); is_prime[] = true; is_prime[0] = is_prime[1] = false; for(int i = 2; i <= n; ++i) { if (is_prime[i]) { for(int j = 2*i; j <= n; j += i) is_prime[j] = false; } } int[] ret; foreach(i;0..n+1) { if (is_prime[i]) ret ~= i; } return ret; } int hash(int x) { if ( x < 10 ) return x; int y = 0; while (x) { y += x%10; x /= 10; } return hash(y); } void solve() { int K,N; readlnToken(K); readlnToken(N); auto tmp = sieve(N); int[] primes; foreach(p;tmp) { if (p < K) continue; primes ~= p; } auto key = primes.map!(hash); int ret = 0; int ans = 0; foreach(i; 0..key.length) { int size = 1; bool[int] exist; exist[key[i]] = true; foreach(j; i+1..key.length) { if ( key[j] in exist ) break; exist[key[j]] = true; size++; } if ( ret < size || (ret == size && ans < primes[i]) ) { ret = size; ans = primes[i]; } } writeln(ans); } void main() { solve(); }