#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

typedef pair<long long int, long long int> P;

int main() {

	vector< vector<long long int> > doketsu(1001, vector<long long int>(1001, 0));

	doketsu[500][500] = 1;

	int x, y;
	x = y = 500;
	x = 501;
	int xR, xL, yT, yB;
	xR = 501;
	xL = 499;
	yT = 499;
	yB = 501;
	int type = 0;
	for (long long int i = 2; i <= 1001 * 1001; i++) {
		doketsu[y][x] = i;
		if (type == 0) {
			if (x < xR) {
				x++;
			}
			else {
				type++;
				y--;
				xR++;
			}
		}
		else if (type == 1) {
			if (yT < y) {
				y--;
			}
			else {
				type++;
				x--;
				yT--;
			}
		}
		else if (type == 2) {
			if (xL < x) {
				x--;
			}
			else {
				type++;
				y++;
				xL--;
			}
		}
		else {
			if (y < yB) {
				y++;
			}
			else {
				type = 0;
				x++;
				yB++;
			}
		}
	}

	vector<int> furui((1001 * 1001) + 1, 0);
	furui[0] = furui[1] = 1;

	for (long long int i = 2; i*i <= 1001 * 1001; i++) {
		if (furui[i] == 0) {
			for (long long int j = 2; i*j <= 1001 * 1001; j++) {
				furui[i*j] = 1;
			}
		}
	}

	/*
	for (int i = 497; i <= 503; i++) {
		for (int j = 497; j <= 503; j++) {
			printf("%2lld ", doketsu[i][j]);
		}
		cout << endl;
	}
	*/
	long long int n, m;
	while (cin >> m >> n) {
		if (n == 0 && m == 0) {
			break;
		}

		vector< vector<P> > dp(1001, vector<P>(1001));

		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				dp[i][j] = P(-1, -1);
				if (doketsu[i][j] == n) {
					dp[i][j] = P(0, 0);
					if (furui[n] == 0) {
						dp[i][j] = P(1, n);
					}
				}
			}
		}
		/*
		for (int i = 497; i <= 503; i++) {
			for (int j = 497; j <= 503; j++) {
				printf("%2lld ", dp[i][j].first);
			}
			cout << endl;
		}
		*/
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				if (dp[i][j].first >= 0) {
					int xx = j;
					int yy = i;
					if (xx - 1 >= 0 && yy + 1 < 1001 && doketsu[yy + 1][xx - 1] <= m) {
						long long int p = 1 - furui[doketsu[yy + 1][xx - 1]];
						if (dp[yy + 1][xx - 1].first < dp[yy][xx].first + p) {
							dp[yy + 1][xx - 1] = dp[yy][xx];
							if (p == 1) {
								dp[yy + 1][xx - 1].first++;
								dp[yy + 1][xx - 1].second = doketsu[yy + 1][xx - 1];
							}
						}
						else if (dp[yy + 1][xx - 1].first == dp[yy][xx].first + p) {
							if (p == 0) {
								dp[yy + 1][xx - 1].second = max(dp[yy + 1][xx - 1].second, dp[yy][xx].second);
							}
						}
					}
					if (yy + 1 < 1001 && doketsu[yy + 1][xx] <= m) {
						long long int p = 1 - furui[doketsu[yy + 1][xx]];
						if (dp[yy + 1][xx].first < dp[yy][xx].first + p) {
							dp[yy + 1][xx] = dp[yy][xx];
							if (p == 1) {
								dp[yy + 1][xx].first++;
								dp[yy + 1][xx].second = doketsu[yy + 1][xx];
							}
						}
						else if (dp[yy + 1][xx].first == dp[yy][xx].first + p) {
							if (p == 0) {
								dp[yy + 1][xx].second = max(dp[yy + 1][xx].second, dp[yy][xx].second);
							}
						}
					}
					if (xx + 1 < 1001 && yy + 1 < 1001 && doketsu[yy + 1][xx + 1] <= m) {
						long long int p = 1 - furui[doketsu[yy + 1][xx + 1]];
						if (dp[yy + 1][xx + 1].first < dp[yy][xx].first + p) {
							dp[yy + 1][xx + 1] = dp[yy][xx];
							if (p == 1) {
								dp[yy + 1][xx + 1].first++;
								dp[yy + 1][xx + 1].second = doketsu[yy + 1][xx + 1];
							}
						}
						else if (dp[yy + 1][xx + 1].first == dp[yy][xx].first + p) {
							if (p == 0) {
								dp[yy + 1][xx + 1].second = max(dp[yy + 1][xx + 1].second, dp[yy][xx].second);
							}
						}
					}
				}
			}
		}

		P ans = P(0, 0);
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				if (dp[i][j].first > ans.first) {
					ans = dp[i][j];
				}
				else if (dp[i][j].first == ans.first) {
					ans.second = max(ans.second, dp[i][j].second);
				}
			}
		}
		/*
		for (int i = 497; i <= 503; i++) {
			for (int j = 497; j <= 503; j++) {
				printf("%2lld ", dp[i][j].first);
			}
			cout << endl;
		}
		*/
		cout << ans.first << ' ' << ans.second << endl;
	}
	return 0;
}


Measured memory_bytes: 30480