import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.TreeSet; public class Main { int hash(int k) { if (k < 10) return k; int sum = 0; while (0 < k) { sum += k % 10; k /= 10; } return hash(sum); } void run() { MyScanner sc = new MyScanner(); int max = 200001; boolean[] prime = new boolean[max]; Arrays.fill(prime, true); prime[0] = prime[1] = false; for (int i = 0; i * i <= max; i++) { if (prime[i]) { for (int j = i * i; j < max; j += i) { prime[j] = false; } } } int n = sc.nextInt(); int m = sc.nextInt(); int count = 0; for (int i = n; i <= m; i++) { if (prime[i]) count++; } int[] list = new int[count]; int[] hash = new int[count]; int id = 0; for (int i = n; i <= m; i++) { if (prime[i]) { list[id] = i; hash[id] = hash(i); id++; } } int left = 0, right = 1; TreeSet set = new TreeSet(); int maxLength = 0; int maxNumber = 0; set.add(hash[left]); while (left < id) { if (id <= right || set.contains(hash[right])) { set.remove(hash[left++]); } else { set.add(hash[right++]); } if (maxLength <= right - left) { maxLength = right - left; maxNumber = list[left]; } } System.out.println(maxNumber); } public static void main(String[] args) { new Main().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------" + '\n'); } class MyScanner { int read() { try { return System.in.read(); } catch (IOException e) { throw new InputMismatchException(); } } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }