Problem: Prime Caves

An international expedition discovered abandoned Buddhist cave temples in a giant cliff standing on the middle of a desert. There were many small caves dug into halfway down the vertical cliff, which were aligned on square grids. The archaeologists in the expedition were excited by Buddha's statues in those caves. More excitingly, there were scrolls of Buddhism sutras (holy books) hidden in some of the caves. Those scrolls, which estimated to be written more than thousand years ago, were of immeasurable value.

The leader of the expedition wanted to collect as many scrolls as possible. However, it was not easy to get into the caves as they were located in the middle of the cliff. The only way to enter a cave was to hang you from a helicopter. Once entered and explored a cave, you can climb down to one of the three caves under your cave; i.e., either the cave directly below your cave, the caves one left or right to the cave directly below your cave. This can be repeated for as many times as you want, and then you can descent down to the ground by using a long rope.

So you can explore several caves in one attempt. But which caves you should visit? By examining the results of the preliminary attempts, a mathematician member of the expedition discovered that (1) the caves can be numbered from the central one, spiraling out as shown in Figure D-1; and (2) only those caves with their cave numbers being prime (let's call such caves prime caves), which are circled in the figure, store scrolls. From the next attempt, you would be able to maximize the number of prime caves to explore.

<image>

Figure D-1: Numbering of the caves and the prime caves


Write a program, given the total number of the caves and the cave visited first, that finds the descending route containing the maximum number of prime caves.

Input

The input consists of multiple datasets. Each dataset has an integer m (1 ≤ m ≤ 106) and an integer n (1 ≤ n ≤ m) in one line, separated by a space. m represents the total number of caves. n represents the cave number of the cave from which you will start your exploration. The last dataset is followed by a line with two zeros.

Output

For each dataset, find the path that starts from the cave n and contains the largest number of prime caves, and output the number of the prime caves on the path and the last prime cave number on the path in one line, separated by a space. The cave n, explored first, is included in the caves on the path. If there is more than one such path, output for the path in which the last of the prime caves explored has the largest number among such paths. When there is no such path that explores prime caves, output "`0 0`" (without quotation marks).

Sample Input


49 22
46 37
42 23
945 561
1081 681
1056 452
1042 862
973 677
1000000 1000000
0 0


Output for the Sample Input


0 0
6 43
1 23
20 829
18 947
10 947
13 947
23 947
534 993541






Example

Input

49 22
46 37
42 23
945 561
1081 681
1056 452
1042 862
973 677
1000000 1000000
0 0


Output

0 0
6 43
1 23
20 829
18 947
10 947
13 947
23 947
534 993541

Solution (in JAVA):

Solution:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	static int[][][] field=new int[1200][1200][2];
	static int SIZE=1200;
	static boolean[] isPrime=new boolean[1000009];
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		makeField();
		makePrimeTable();
		for(;;){
			
			int num=sc.nextInt();
			int start=sc.nextInt();
			if(start+num==0)break;
			int x=0,y=0;
			label:for(int i=0;i<SIZE;i++){
				for(int j=0;j<SIZE;j++){
					if(field[i][j][0]==start){
						x=j;
						y=i;
						break label;
					}
				}
			}
			Queue<int[]>q=new LinkedList<int[]>();
			int v=0;
			if(isPrime[field[y][x][0]])v=1;
			q.add(new int[]{y,x,v});
			field[y][x][1]=v;
			while(q.isEmpty()==false){
				int[]a=q.poll();
				y=a[0];x=a[1];
				v=a[2];
//				System.out.println(field[y][x][1]+" "+v);
				if(field[y][x][1]>v)continue;
				y++;
				for(int i=-1;i<=1;i++){
					int xx = x+i;
					if(field[y][xx][0]>num)continue;
					if(isPrime[field[y][xx][0]])v++;
					if(field[y][xx][1]<v){
						field[y][xx][1]=v;
						q.add(new int[]{y,xx,field[y][xx][1]});
					}
					if(isPrime[field[y][xx][0]])v--;
				}
				/*
				// down left
				y++;
				x--;
				System.out.println(field[y][x][0]);
				if(field[y][x][0]<=num&&field[y][x][1]<v){
					field[y][x][1]=v;
					if(isPrime[field[y][x][0]])field[y][x][1]++;
					q.add(new int[]{y,x,field[y][x][1]});
				}
				// down
				x++;
				System.out.println(field[y][x][0]);
				if(field[y][x][0]<=num&&field[y][x][1]<v){
					field[y][x][1]=v;
					if(isPrime[field[y][x][0]])field[y][x][1]++;
					q.add(new int[]{y,x,field[y][x][1]});
				}
				// down right
				x++;
				System.out.println(field[y][x][0]);
				if(field[y][x][0]<=num&&field[y][x][1]<v){
					field[y][x][1]=v;
					if(isPrime[field[y][x][0]])field[y][x][1]++;
					q.add(new int[]{y,x,field[y][x][1]});
				}
				*/
			}
			int max=-1;
			int ans=-1;
			for(int i=0;i<SIZE;i++){
				for(int j=0;j<SIZE;j++){
					if(field[i][j][0]>num)continue;
					if(isPrime[field[i][j][0]]==false)continue;
					if(max<field[i][j][1]){
						max=field[i][j][1];
						ans=field[i][j][0];
					}
					else if(max==field[i][j][1]){
						ans=Math.max(field[i][j][0],ans);
					}
				}
			}
			if(max==-1){
				System.out.println("0 0");
			}
			else
				System.out.println(max+" "+ans);
			for(int i=0;i<SIZE;i++){
				for(int j=0;j<SIZE;j++){
					field[i][j][1]=-2;
				}
			}
		}
	}
	static void makePrimeTable(){
		Arrays.fill(isPrime, true);
		isPrime[0]=isPrime[1]=false;
		for(int i=2;i*i<=isPrime.length;i++){
			if(isPrime[i]){
				for(int j=i+i;j<isPrime.length;j+=i)isPrime[j]=false;
			}
		}
	}
	static void makeField(){
		int x = field.length/2;
		int y = field.length/2;
		int num=1;
		for(int i=0;i<SIZE;i++){
			for(int j=0;j<SIZE;j++){
				field[i][j][0]=1<<29;
				field[i][j][1]=-2;
			}
		}
		field[x][y][0]=num++;
		for(int n=1;;n+=2){
			if(num>1100000)break;
			// right
			for(int i=0;i<n;i++){
				x++;
				field[y][x][0]=num++;
			}
			// up
			for(int i=0;i<n;i++){
				y--;
				field[y][x][0]=num++;
			}
			// left
			for(int i=0;i<n+1;i++){
				
				x--;
				field[y][x][0]=num++;
			}
			// down
			for(int i=0;i<n+1;i++){
				y++;
				field[y][x][0]=num++;
			}
		}

	}
}