#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define reps(i, n, m) for (int i = (int) (n); i < (int) (m); i++)

vector<vector<pair<int, int>>> dp;
vector<vector<int>> num;
vector<bool> P(1123456, true);
int n, m;
int sr, sc;
pair<int, int> ans;

bool init() {
	dp.clear();
	num.clear();
	ans = make_pair(0, 0);
	cin >> m >> n;
	if (m == 0) return false;
	dp.resize(1111, vector<pair<int, int>>(1111, make_pair(-1, 0)));
	num.resize(1111, vector<int>(1111, 0));
	int dr[] = {0, -1, 0, 1};
	int dc[] = {1, 0, -1, 0};
	int p = 2;
	int r = 505;
	int c = 505;
	reps(i, 1, m+1) {
		int np = (p + 1) % 4;
		if (num[r + dr[np]][c + dc[np]] == 0) {
			r += dr[np];
			c += dc[np];
			p = np;
		} else {
			r += dr[p];
			c += dc[p];
		}
		num[r][c] = i;
		if (i == n) {
			sr = r;
			sc = c;
		}
	}
	return true;
}

pair<int, int> dfs(int r, int c) {
	pair<int, int>& res = dp[r][c];
	if (res.first >= 0) return res;
	if (num[r][c] == 0) return res = make_pair(0, 0);
	res = max(dfs(r+1, c-1), dfs(r+1, c));
	res = max(res, dfs(r+1, c+1));
	res.first += P[num[r][c]];
	if (P[num[r][c]] && res.first == 1) res.second = num[r][c];
	return res;
}

void solve() {
	auto res = dfs(sr, sc);
	cout << res.first << " " << res.second << endl;
}

int main() {
	reps(i, 2, 1123456) {
		if (P[i]) {
			for (int j = i + i; j < 1123456; j += i) {
				P[j] = false;
			}
		}
	}
        P[1] = false;
	while (init()) {
		solve();
	}
}
