import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int k = sc.nextInt(); int n = sc.nextInt(); boolean[] isNotPrime = new boolean[n + 1]; HashSet used = new HashSet<>(); ArrayDeque primes = new ArrayDeque<>(); int max = 0; int maxIdx = 0; int left = k; for (int i = 2; i <= n; i++) { if (isNotPrime[i]) { continue; } for (int j = 2; j * i <= n; j++) { isNotPrime[j * i] = true; } if (i < k) { continue; } int hash = getHash(i); if (used.contains(hash)) { while (true) { int remain = getHash(primes.poll()); if (hash == remain) { break; } used.remove(remain); } } primes.add(i); used.add(hash); if (max <= primes.size()) { max = primes.size(); maxIdx = primes.peek(); } } System.out.println(maxIdx); } static int getHash(int x) { if (x < 10) { return x; } else { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return getHash(sum); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public String nextLine() throws Exception { return br.readLine(); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }