import java.util.*; public class No6 { public static boolean checkPrime (int n) { boolean b = true; if (n==2 || n==3 || n==5) ; else if ((n & 1)==0 || n==1) b = false; else { for (int i = 3; i < n/2; i+=2) { if (n % i==0) { b = false; break; } } } return b; } public static int sumEachbit (int n) { int sum; while (n > 9) { sum = 0; for (; n > 0;) { sum += n%10; n /= 10; } n = sum; } return n; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int K = scan.nextInt(); int N = scan.nextInt(); int[] S = new int[N+1]; int check[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int ans = 0, max = 0, tmp1 = 0, tmp2 = 0, tmp3 = 0; for (int i = K; i <= N; i++) { if (checkPrime(i)) { S[i] = i; if (S[i] > 9) S[i] = sumEachbit(i); } else S[i] = -1; } for (int i = K; i <= N; i++) { if (S[i] == -1); else { if (tmp1 == 1) tmp3 = i; if (check[S[i]] == 0) { if (tmp1 == 0) tmp2 = i; tmp1++; check[S[i]] = 1; } else { if (tmp1 >= max) { max = tmp1; ans = tmp2; } tmp1 = 0; Arrays.fill(check, 0); i = tmp3 - 1; } } } if (tmp1 >= max) ans = tmp2; System.out.println(ans); } }