import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int k = sc.nextInt(); int n = sc.nextInt(); HashSet hashes = new HashSet<>(); ArrayDeque deq = new ArrayDeque<>(); boolean[] isNotPrimes = new boolean[n + 1]; int max = 0; int ans = 0; for (int i = 2; i <= n; i++) { if (isNotPrimes[i]) { continue; } for (int j = 2; j * i <= n; j++) { isNotPrimes[j * i] = true; } if (i < k) { continue; } int h = getHash(i); if (hashes.contains(h)) { int x; while (h != (x = getHash(deq.poll()))) { hashes.remove(x); } deq.add(i); } else { hashes.add(h); deq.add(i); } if (max <= hashes.size()) { max = hashes.size(); ans = deq.peek(); } } System.out.println(ans); } 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 next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }