#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int ban[1010][1010][2];
int M, N;
int D[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
bool isprime[1000010];
int px = 500;
int py = 500;


void solve() {
	int ans = 0;
	int ansp = 0;
	bool f = true;
	int cnt = 0;
	while(f) {
		f = false;
		for (int px2 = max(px - cnt, 0); px2 <= min(px + cnt, 1000); px2++) {
			int p = ban[py][px2][0];
			if (p == 0 or p > M) continue;
			f = true;
			if (isprime[p]) {
				ban[py][px2][1] += 1;
				if (ans < ban[py][px2][1] or (ans == ban[py][px2][1] and ansp < p)){
					ans = ban[py][px2][1];
					ansp = p;
				}
			}
			for (int j = -1; j <= 1; j++) {
				if(px2 + j < 0) continue;
				ban[py + 1][px2 + j][1] = max(ban[py + 1][px2 + j][1], ban[py][px2][1]);
			}
		}
		py ++;
		cnt ++;
	}
	cout << ans << " " << ansp << endl;
}


int main() {
	rep(i, 1000001) isprime[i] = true;
	for (int i = 2; i < 1000001; i++) {
		if (isprime[i]) {
			for(int i2 = i * 2; i2 < 1000001; i2 += i) {
				isprime[i2] = false;
			}
		}
	}
	isprime[1] = false;
	isprime[0] = false;
	int k = 1;
	int m = 1;
	int d = 0;
	ban[py][px][0] = k;
	while(k < 1000001) {
		rep(i, m) {
			k += 1;
			px += D[d][0];
			py += D[d][1];
			ban[py][px][0] = k;
		}
		if (d == 1 or d == 3) m ++;
		d = (d + 1) % 4;
	}
	cin >> M >> N;
	while (M) {
		rep(i, 1001) {
			rep(j, 1001) {
				ban[i][j][1] = 0;
			}
		}
		rep(i, 1001) {
			rep(j, 1001) {
			if(ban[i][j][0] == N) {
				px = j; 
				py = i;
				i = 100000;
				break;
			}}}
		solve();
		cin >> M >> N;
	}
}

