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 CPP):

Solution:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <sstream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
 
using namespace std;
 
#define ll long long
#define pi pair<int,int>
#define pll pair<ll,ll>
#define pii pair<int,pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x)<0?(-(x)):(x))
#define xx(x) ((x)*(x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(),(x).end()
#define Max (1<<30)
#define LLMax (1ll<<60)
template<class T>string ToString(T t){stringstream s;s<<t;return s.str();}
template<class T>void ToOther(T&t,string a){stringstream s(a);s>>t;}
 

 
int a[3333][3333];

int xx[]={1,0,-1,0};
int yy[]={0,-1,0,1};

int wh1[1000005];
int wh2[1000005];
int d[3333][3333];
int ck[3333][3333];

bool pr[1000005];

int main(){
//	freopen("output.txt","w",stdout);

	pr[1]=1;
	for(int i=2;i<=1000000;i++)if(pr[i]==0){
		pr[i]=0;
		for(int j=i+i;j<=1000000;j+=i)pr[j]=1;
	}

	int x=1500;
	int y=1500;
	int di=0,now=1;

	a[y][x]=now;
	wh1[now]=y;
	wh2[now]=x;

	for(int L=1;;L++){
		
		for(int i=0;i<2;i++){
			for(int c=0;c<L;c++){
				now++;
				if(now>1000000)goto END;
				x+=xx[di];
				y+=yy[di];

				a[y][x]=now;
				wh1[now]=y;
				wh2[now]=x;

			}
			di=(di+1)%4;
		}
	} 
	END:;

	int n,st;
	int CC=0;
	while(cin>>n>>st,n){
		CC++;
		int mx=Max;
		int MX=-Max;
		int my=Max;
		int MY=-Max;

		for(int i=1;i<=n;i++){
			mx=min(mx,wh2[i]);
			MX=max(MX,wh2[i]);

			my=min(my,wh1[i]);
			MY=max(MY,wh1[i]);
		}

		int x=wh2[st];
		int y=wh1[st];

		ck[y][x]=CC;
		d[y][x]=!pr[st];

		int r1=0;
		int r2=0;

		if(!pr[st])r1=1,r2=st;

		for(int i=y+1;i<=MY;i++){
			for(int j=mx;j<=MX;j++)if(a[i][j]>=1 && a[i][j]<=n){
				for(int k=-1;k<=1;k++){
					int nx=j+k;

					if(ck[i-1][nx]!=CC)continue;
					if(a[i-1][nx]<=0 || a[i-1][nx]>n)continue;

					if(ck[i][j]==CC){
						d[i][j]=max(d[i][j],d[i-1][nx]+ !pr[ a[i][j] ]);
					}else d[i][j]=d[i-1][nx]+ !pr[ a[i][j] ];
					ck[i][j]=CC;

					if(!pr[ a[i][j] ]){
						if(r1<d[i][j]){
							r1=d[i][j];
							r2=a[i][j];
						}else if(r1==d[i][j])r2=max(r2,a[i][j]);
					}
				}
			}
		}

		cout<<r1<<" "<<r2<<endl;
	}
}