public static void main(String[] args) throws Throwable{
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 
		boolean[] p=new boolean[1120];primos=new int[187];
		p[0]=p[1]=true;int c=0;mem=new int[1121][187][15];
		for(int [][]m:mem)for(int []r:m)Arrays.fill(r,-1);
		for(int i=0;i<p.length;i++)if(!p[i]){primos[c++]=i;for(int j=2*i;j<p.length;j+=i)p[j]=true;}
		for(StringTokenizer st;(st=new StringTokenizer(in.readLine()))!=null;){
			int K=parseInt(st.nextToken()),J=parseInt(st.nextToken());
			if(K==0&&J==0)break;
			System.out.println(f(K,186,J));
		}
	}