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 diff = Integer.MIN_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 tmpCount = 0; 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 { tmpCount++; tmpMax = j; map.put(d[j], j); } } if (count <= tmpCount && diff <= tmpMax - i) { count = tmpCount; diff = tmpMax - i; min = i; } } System.out.println(min); } private static int change(int source) { char[] c = Integer.toString(source).toCharArray(); while (c.length != 1) { source = 0; if (c.length > 1) { for (int i = 0; i < c.length; i++) { source += Integer.parseInt(String.valueOf(c[i])); } } else { source += Integer.parseInt(String.valueOf(c)); break; } c = Integer.toString(source).toCharArray(); } return source; } }