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(); boolean[] nera = new boolean[N+1]; Arrays.fill(nera, true); for(int i=2;i*i<=N;i++){ if(nera[i]){ for(int j=i*2;j<=N;j+=i){ if(nera[j])nera[j]=false; } } } ArrayList sosuu = new ArrayList<>(); ArrayList ha = new ArrayList<>(); for(int i=K;i<=N;i++){ if(i==1) continue; if(nera[i]){ int temp=i; sosuu.add(i); while(temp>9){ int temp2=temp; temp=0; while(temp2!=0){ temp+=temp2%10; temp2=temp2/10; } } ha.add(temp); } } int t=0; int s=0; Queue queue = new LinkedList<>(); int ans=0; int ansmemo=0; int temp=0; int max=0; while(true){ while(t=ans){ ans=t-s; max=sosuu.get(s); } break; } if(t==ha.size()-1){ int temp1=ha.get(t); queue.offer(temp1); if(t-s+1>=ans){ max=sosuu.get(s); } System.out.println(max); return; } queue.offer(ha.get(t++)); } while(true){ int temp1=queue.poll(); s++; if(temp1==ha.get(t)){ break; } } } } }