import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int K = Integer.parseInt(br.readLine()); int N = Integer.parseInt(br.readLine()); int sc = (int) Math.sqrt(N); int[] d = new int[N + 1]; d[0] = -1; d[1] = -1; for (int i = 2; i <= sc; i++) { if (d[i] != 0) { continue; } for (int j = i; j < N; j++) { int dest = i * j; if (dest > d.length - 1) { break; } d[i * j] = -1; } } int tmpMax = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; int count = 0; for (int i = K; i <= N; i++) { if (d[i] == -1) { continue; } if (d[i] == 0) { d[i] = change(i); } HashMap map = new HashMap(); map.put(d[i], i); int j = i + 1; for (; j <= N; j++) { if (d[j] == -1) { continue; } else if (d[j] == 0) { d[j] = change(j); } if (map.containsKey(d[j])) { break; } else { tmpMax = j; map.put(d[j], j); } } if (count <= map.size()) { count = map.size(); min = i; } } System.out.println(min); } private static int change(int source) { if (source < 10) { return source; } int sum = 0; while (0 < source) { sum += source % 10; source /= 10; } return change(sum); } }