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){ max=sosuu.get(s); } System.out.println(max); return; } if(flag){ if(t-s-1>=ans){ ans=t-s-1; max=sosuu.get(s); } break; } } while(true){ int temp2=queue.poll(); s++; if(temp2==temp){ temp=0; break; } } } } }