#include <iostream>
#include <algorithm>
#include <string>

#define MAX 1000001
#define SIZE 2000

typedef long long ll;

using namespace std;

int m, n;
int table[SIZE][SIZE]={{0,0}};
int dp[SIZE][SIZE];
bool prime[MAX]; 
pair<int, int>coord[MAX];//座標
pair<int, int> ans;// <素数の数, 出口の番号>


void dfs(int count, int y, int x){
	int num = table[y][x];
	if(num <= 0 || m < num) return;
	if(count < dp[y][x] || (count == dp[y][x] && prime[num] == false && dp[y][x] !=0)) return;
	dp[y][x] = count;

	if(prime[num] == true){
		count++;
		dp[y][x] = count;
		if(ans.first < count || (ans.first == count && ans.second < num)){
			ans.first = count;
			ans.second = num;
		}
	}

	for(int dx=-1; dx<=1; dx++){
		dfs(count, y+1, x+dx);
	}
}


int main(void){
	fill(prime, prime+MAX, true);
	prime[0] = prime[1] = false;
	for(int i=2; i<MAX; i++){
		if(prime[i]){
			for(int j=i+i; j<MAX; j+=i){
				prime[j] = false;
			}
		}
	}

	int d=0, x=1000, y=1000;
	int dx[4]={0, 1, 0, -1};
	int dy[4]={1, 0, -1, 0};
	for(int i=1; i<MAX; i++){
		table[y][x] = i;
		coord[i] = make_pair(y, x);
		if(table[y+dy[(d+1)%4]][x+dx[(d+1)%4]] == 0) d++;
		x += dx[d%4];
		y += dy[d%4];
	}

	while(1){
		cin >> m >> n;
		if(!m && !n) break;

		int y = coord[n].first;
		int x = coord[n].second;
		fill(dp[0], dp[2000], 0);
		ans = make_pair(0, 0);
		dfs(0, y, x);
		cout << ans.first << " " << ans.second << endl;
	}

	return 0;
}
