import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; public class Main { public static final int hash(int n){ if(n < 10){ return n; }else{ int ret = 0; for(final char c : (n + "").toCharArray()){ ret += Character.getNumericValue(c); } return hash(ret); } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); final int K = sc.nextInt(); final int N = sc.nextInt(); boolean[] is_prime = new boolean[N + 1]; Arrays.fill(is_prime, true); is_prime[0] = is_prime[1] = false; LinkedList primes = new LinkedList(); for(int p = 2; p <= N; p++){ if(is_prime[p]){ if(p >= K){ primes.add(p); } for(int j = 2 * p; j <= N; j += p){ is_prime[j] = false; } } } boolean[] hash_use = new boolean[10]; LinkedList hash_using = new LinkedList(); LinkedList prime_using = new LinkedList(); int max_len = 1, min = Integer.MAX_VALUE; for(final int prime : primes){ final int hash = hash(prime); if(hash_use[hash]){ while(true){ final int removed_prime = prime_using.poll(); if(max_len <= hash_using.size()){ max_len = hash_using.size(); min = removed_prime; } final int removed_hash = hash_using.poll(); hash_use[removed_hash] = false; if(removed_hash == hash){ break; } } } hash_using.add(hash); prime_using.add(prime); hash_use[hash] = true; //System.out.println(hash + " : " + hash_using + "=" + prime_using + " " + prime + " " + min); } if(!hash_using.isEmpty() && max_len <= hash_using.size()){ max_len = hash_using.size(); min = prime_using.getFirst(); } System.out.println(min); } }