#include<bits/stdc++.h>
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

bool is_prime[1000001];
int t[1500][1500], dp[1500][1500];
int dx[]{ 1,0,-1,0 }, dy[]{ 0,1,0,-1 };

int main() {
	memset(is_prime, 1, sizeof(is_prime));
	is_prime[0] = is_prime[1] = false;
	for (int i = 2; i <= 1000000; i++) {
		if (is_prime[i]) {
			for (int j = i * 2; j <= 1000000; j += i)is_prime[j] = false;
		}
	}
	int m, n;
	while (scanf("%d%d", &m, &n), m) {
		memset(dp, 0, sizeof(dp));
		memset(t, 0, sizeof(t));
		int sx, sy, x = 750, y = 750, id = 0;
		for (int i = 1; i <= m; i++) {
			t[x][y] = i;
			if (i == n)sx = x, sy = y;
			int nx = x + dx[(id + 1) % 4], ny = y + dy[(id + 1) % 4];
			if (!t[nx][ny]) {
				(id += 1) %= 4; x = nx; y = ny;
			}
			else x += dx[id], y += dy[id];
		}
		int Max = 0; id = 0;
		if (is_prime[n])dp[sx][sy] = 1;
		for (int i = sx; i < 1400; i++) {
			for (int j = sy - (i - sx); j <= sy + (i - sx); j++) {
				if (Max < dp[i][j] && is_prime[t[i][j]]) {
					Max = dp[i][j]; id = t[i][j];
				}
				else if (Max == dp[i][j] && is_prime[t[i][j]]) {
					id = max(id, t[i][j]);
				}
				for (int k = j - 1; k <= j + 1; k++) {
					if (is_prime[t[i + 1][k]])dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + 1);
					else dp[i + 1][k] = max(dp[i + 1][k], dp[i][j]);
				}
			}
		}
		if (Max == 0)puts("0 0");
		else printf("%d %d\n", Max, id);
	}
}