#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

int dp[1003][1003];
int visit[1003][1003];
int cave[1003][1003] = { 0 };
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, -1,  0, 1 };

int main() {
	ifstream fin("input.txt");
	ofstream fout("output.txt");

	//1~1000000の内素数ならtrue素数でないならfalse
	static int isPrime[1000001];
	for (int i = 0; i < 1000001; i++) {
		isPrime[i] = 1;
	}
	isPrime[0] = 0;
	isPrime[1] = 0;
	for (int i = 2; i <= 1000; i++) {
		if (isPrime[i]) {
			for (int j = 2; i * j < 1000001; j++) {
				isPrime[i * j] = 0;
			}
		}
	}

	int c = 1, d = 0;
	int x = 500, y = 500;
	bool end = false;
	while (true) {
		for (int dir = 0; dir < 4; dir++) {
			if (dir % 2 == 0) d++;
			for (int cnt = 1; cnt <= d; cnt++) {
				cave[x][y] = c;
				if (c == 1000000) {
					end = true;
					break;
				}
				c++;
				x = x + dx[dir];
				y = y + dy[dir];
			}
			if (end) break;
		}
		if (end) break;
	}

	while (true) {
		int all, num = 0, first, last = 0, f_x, f_y, l_x, l_y;
		cin >> all >> first;
		if (all == 0 && first == 0) break;

		for (int j = 0; j < 1003; j++) {
			for (int i = 0; i < 1003; i++) {
				dp[i][j] = 0;
				visit[i][j] = 0;
				if (cave[i][j] == first) {
					f_x = i;
					f_y = j;
					visit[i][j] = 1;
					if (isPrime[first] == 1) dp[i][j] = 1;
				}
				if (cave[i][j] == all) {
					l_x = i;
					l_y = j;
				}
			}
		}

		for (int y = f_y + 1; y <= 1002; y++) {
			for (int x = 1; x <= 1002; x++) {
				if (cave[x][y] <= all && cave[x][y] != 0) {
					if (!(visit[x - 1][y - 1] == 0 && visit[x][y - 1] == 0 && visit[x + 1][y - 1] == 0)) {
						dp[x][y] = max(visit[x - 1][y - 1] * dp[x - 1][y - 1], max(visit[x][y - 1] * dp[x][y - 1], visit[x + 1][y - 1] * dp[x + 1][y - 1]))
							+ isPrime[cave[x][y]];
						visit[x][y] = 1;
					}
				}
			}
		}

		for (int y = f_y; y <= 1002; y++) {
			for (int x = 1; x <= 1002; x++) {
				num = max(num, dp[x][y]);
			}
		}

		if (num != 0) {
			for (int y = f_y; y <= 1002; y++) {
				for (int x = 1; x <= 1002; x++) {
					if (dp[x][y] == num && isPrime[cave[x][y]] == 1) {
						last = max(last, cave[x][y]);
					}
				}
			}
		}
		//cout << endl;
		//for (int y = 0; y <= 10; y++) {
		//	for (int x = 0; x <= 10; x++) {
		//		cout << dp[x][y] << " ";
		//	}
		//	cout << endl;
		//}
		//cout << endl;
		//for (int y = 0; y <= 10; y++) {
		//	for (int x = 0; x <= 10; x++) {
		//		cout << visit[x][y] << " ";
		//	}
		//	cout << endl;
		//}

		cout << num << " " << last << endl;
	}

	return 0;
}