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.Collections;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	
	public static final int MAX = 1000000;
	public static final int SQRT = 1000;
	
	public static final int MAP_SIZE = 2 * SQRT + 1;
	public static final int CENTER = SQRT + 1;
	
	public static int[][] move_dir = new int[][]{{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
	
	public static void circle(int[][] map, int m, int[] x_pos, int[] y_pos){
		int len = 1;
		int count = 1;
		int type = 0;
		int cur_x = CENTER;
		int cur_y = CENTER;
		
		x_pos[count] = cur_x;
		y_pos[count] = cur_y;
		map[cur_x][cur_y] = count++;
		prepare:
		while(true){
			final int dx = move_dir[type][0];
			final int dy = move_dir[type][1];
			
			for(int i = 0; i < len; i++){
				cur_x += dx;
				cur_y += dy;
				
				if(count > m){
					break prepare;
				}
				
				x_pos[count] = cur_x;
				y_pos[count] = cur_y;
				map[cur_y][cur_x] = count++;
			}
			
			type = (type + 1) % move_dir.length;
			
			if(type % 2 == 0){
				len++;
			}
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		/* generate primes */
		boolean is_prime[] = new boolean[MAX + 1];
		Arrays.fill(is_prime, true);
		is_prime[0] = is_prime[1] = false;
		
		for(int i = 2; i <= SQRT; i++){
			if(is_prime[i]){
				for(int j = 2 * i; j <= MAX; j += i){
					is_prime[j] = false;
				}
			}
		}
		
		int[][] map = new int[MAP_SIZE][MAP_SIZE];
		int[] x_pos = new int[MAX + 1];
		int[] y_pos = new int[MAX + 1];
		
		int[][] dp_prev_prime = new int[MAP_SIZE][MAP_SIZE];
		int[][] dp_length = new int[MAP_SIZE][MAP_SIZE];
		
		while (true) {
			final int m = sc.nextInt();
			final int n = sc.nextInt();
			
			if(m == 0 && n == 0){
				break;
			}
			
			Arrays.fill(x_pos, 0);
			Arrays.fill(y_pos, 0);
			for(int i = 0; i < MAP_SIZE; i++){
				Arrays.fill(map[i], 0);
			}
			
			circle(map, m, x_pos, y_pos);
			
			for(int y = MAP_SIZE - 1; y >= 0; y--){
				Arrays.fill(dp_length[y], 0);
				Arrays.fill(dp_prev_prime[y], 0);
			}
			
			
			for(int y = MAP_SIZE - 1; y >= 0; y--){
				for(int x = MAP_SIZE - 1; x >= 0; x--){
					final int cave_value = map[y][x];
					
					if(cave_value == 0){
						continue;
					}else if(cave_value > m){
						continue;
					}
					
					if(is_prime[cave_value]){
						dp_length[y][x]++;
						
						if(dp_prev_prime[y][x] == 0){
							dp_prev_prime[y][x] = cave_value;
						}
					}
					
					if(y == 0){
						continue;
					}
					
					//System.out.println(cave_value + " " + dp_length[y][x] + " " + dp_prev_prime[y][x]);
					//sc.nextLine();
					final int next_y = y - 1;
					for(int dx = -1; dx <= 1; dx++){
						final int next_x = x + dx;
						final int next_value = map[next_y][next_x];
						
						if(next_x < 0){
							continue;
						}else if(next_x >= MAP_SIZE){
							continue;
						}else if(next_value == 0){
							continue;
						}else if(next_value > m){
							continue;
						}
						
						final int cave_length = dp_length[y][x];
						final int next_length = dp_length[next_y][next_x];
						
						final int cave_prev_prime = dp_prev_prime[y][x];
						final int next_prev_prime = dp_prev_prime[next_y][next_x];
						
						if(cave_length > next_length || (cave_length == next_length && cave_prev_prime > next_prev_prime)){
							dp_length[next_y][next_x] = cave_length;
							dp_prev_prime[next_y][next_x] = cave_prev_prime;
						}
					}
				}
			}
			
			//System.out.println(y_pos[n] + " " + x_pos[n]);
			
			System.out.println(dp_length[y_pos[n]][x_pos[n]] + " " + dp_prev_prime[y_pos[n]][x_pos[n]]);	
		}
		
		sc.close();
	}
}