import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); int n = sc.nextInt(); HashSet used = new HashSet<>(); ArrayDeque deq = new ArrayDeque<>(); int max = 0; int ans = 0; boolean[] isNotPrime = new boolean[n + 1]; for (int i = 2; i <= n; i++) { if (!isNotPrime[i]) { for (int j = 2; j * i <= n; j++) { isNotPrime[j * i] = true; } if (i < k) { continue; } int h = hash(i); if (used.contains(h)) { while (deq.size() > 0) { int x = deq.poll(); if (hash(x) == h) { break; } used.remove(i); } } used.add(h); deq.add(i); if (max <= deq.size()) { max = deq.size(); ans = deq.getFirst(); } } } System.out.println(ans); } static int hash(int x) { if (x < 10) { return x; } else { int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return hash(sum); } } }