import java.io.*; import java.util.*; import java.math.*; class Main6 { public static void out (Object out) { System.out.println(out); } public static int hash (int p) { int ret = 0; while (p != 0) { ret += p % 10; p /= 10; } return ret < 10 ? ret : hash(ret); } public static boolean[] sieve (int n) { boolean[] ret = new boolean[n + 1]; Arrays.fill(ret , true); ret[1] = false; for (int i = 4; i <= n; i += 2) ret[i] = false; for (int i = 3; i * i <= n; i += 2) { for (int j = 3; i * j <= n; j += 2) { ret[i * j] = false; } } return ret; } public static void main (String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int k = Integer.parseInt(br.readLine()); int n = Integer.parseInt(br.readLine()); boolean[] isPrime = sieve(n); int[] hashes = new int[n + 1]; //out("Prime---"); for (int i = k; i <= n; i++) { if (!isPrime[i]) continue; hashes[i] = hash(i); //out(i + "\thash(i) = " + hashes[i]); } //out("---"); int startP = 0; int ans = 0; int maxLen = 0; ArrayList list = new ArrayList(); for (int i = k; i <= n; i++) { if (!isPrime[i]) continue; startP = i; list.add(hashes[i]); for (int j = i + 1; j <= n; j++) { if (!isPrime[j]) continue; if (!list.contains(hashes[j])) { list.add(hashes[j]); continue; } break; } if (maxLen <= list.size()) { maxLen = list.size(); ans = startP; //out("ans更新 : " + ans); } list.clear(); } out(ans); } }