#include<stdio.h>
#include<math.h>
#define M 1000000
#define N 1000
#define ROOP(i,a,b) for(i = a;i < b;i++) //* [a,b)

//* important!!!
//* dp[y(i)][x(j)]
int dp[N+2][N+2];
int board[N+2][N+2],prime[M+1];

void init();
void print();
	
int main(){
	int i,j,k,l,m,n;
	int x,y;
	int cnt,ans,max;
	int q;
	//int* tmp1;
	//int* tmp2;

	init();
	//print();

	while(scanf("%d%d",&m,&n),m+n){
		ROOP(x,1,N+1)ROOP(y,1,N+1)if(board[y][x] == n)goto BREAK;
		BREAK:

		dp[y][x] = (prime[n] == 1);
		max = dp[y][x];
		ans = (max?n:0);
		q = sqrt(m-1)/2+1;

		i = y;
		while(i < q+501 && i < N){
			j = x-(i-y);
			if(j <= 0)j = 1;
			while(j <= x+(i-y) && j <= N){//
				ROOP(k,-1,2){
					//tmp1 = &(board[i+1][j+k]);
					//tmp2 = &(dp[i+1][j+k]);
					if(board[i+1][j+k] > m)continue;
					if(j+k == 0 || j+k == 1001)continue;//
					if(dp[i][j]+prime[board[i+1][j+k]] > dp[i+1][j+k])
						dp[i+1][j+k] = dp[i][j]+prime[board[i+1][j+k]];
					if(max == dp[i+1][j+k] && prime[board[i+1][j+k]])
						if(ans < board[i+1][j+k])
							ans = board[i+1][j+k];
					if(max < dp[i+1][j+k]){
						max = dp[i+1][j+k];
						ans = board[i+1][j+k];
					}
				}
				j++;
			}
			i++;
		}
		printf("%d %d\n",max,ans);
	}
	return 0;
}

void init(){
	int i,j;
	int dir,x,y,nowx,nowy;
	int vecx[4] = { 1, 0,-1, 0};
	int vecy[4] = { 0, 1, 0,-1};
	
	//* set prime
	prime[2] = 1;
	for(i = 3;i <= M;i += 2)prime[i] = 1;
	for(i = 3;i*i <= M;i += 2)
		if(prime[i])for(j = i*i;j <= M;j += i)prime[j] = 0;

	//* make wall around the board
	ROOP(i,0,N+2){
		board[0][i] = 1;
		board[i][0] = 1;
		board[N+1][i] = 1;
		board[i][N+1] = 1;
	}

	//* set board
	dir = 0;x = 0;y = 1;
	for(i = M;i >= 0;i--){	//* fill from the outside
		nowx = x+vecx[dir];
		nowy = y+vecy[dir];
		if(board[nowy][nowx]){//* if hit the wall
			dir = (dir + 1)%4;
			nowx = x+vecx[dir];
			nowy = y+vecy[dir];
		}
		board[nowy][nowx] = i;
		x = nowx;y = nowy;
	}
}

void print(){
	int i,j;
	int m = 1039,n = 1;
	int q;
	int x,y;

	ROOP(x,1,N+1)ROOP(y,1,N+1)if(board[y][x] == n)goto BREAK;
	BREAK:

	q = sqrt(m)/2+1;printf("%d %d\n\n",y,x);
	for(i = 0;y+i < 501+q;i++){
		for(j = 0;x+j < 501+q;j++){
			printf("%4d ",board[y+i][x+j]);
		}puts("");
	}
}













				