#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return (a > b && (a = b, true)); }
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return (a < b && (a = b, true)); }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	os << v[0];
	for (int i = 1; i < v.size(); i++) os << " " << v[i];
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u; return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}

bitset<1000005> prime;

int idx[3005][3005];
bool solve() {
	memset(idx, -1, sizeof(idx));
	int off = 1500;

	int M, N; cin >> M >> N;
	if (M == 0 && N == 0) return (false);
	vector<Pi> point(M+1);

	idx[off][off] = 1;
	point[1] = Pi(0, 0);	

	int cnt = 2;
	Pi now = Pi(1, 0);
	for (int i = 2; cnt <= M; i += 2) {
		for (int j = 0; cnt <= M && j < i-1; j++) {
			idx[now.second+off][now.first+off] = cnt;
			point[cnt] = now;
			now.second--;
			cnt++;
		}
		for (int j = 0; cnt <= M && j < i; j++) {
			idx[now.second+off][now.first+off] = cnt;
			point[cnt] = now;
			now.first--;
			cnt++;
		}
		for (int j = 0; cnt <= M && j < i; j++) {
			idx[now.second+off][now.first+off] = cnt;
			point[cnt] = now;
			now.second++;
			cnt++;
		}
		for (int j = 0; cnt <= M && j < i+1; j++) {
			idx[now.second+off][now.first+off] = cnt;
			point[cnt] = now;
			now.first++;
			cnt++;
		}
	}
	
	//cout << "A" << endl;
	vvint G(M+1);
	for (int i = 1; i <= M; i++) {
		int kx[3] = {-1, 0, 1};
		int ky[3] = {1, 1, 1};
		int y = point[i].second;
		int x = point[i].first;
		//cout << i << " : " << y << " " << x << endl;
		for (int j = 0; j < 3; j++) {
			int ny = y + ky[j];
			int nx = x + kx[j];
			if (idx[ny+off][nx+off] != -1) {
				G[i].push_back(idx[ny+off][nx+off]);
			}
		}
	}
	
	vector<Pi> dp(M+1, Pi(-INF, -INF));
	if (prime[N]) dp[N] = Pi(1, N);
	else dp[N] = Pi(0, -INF);

	queue<int> que;
	que.push(N);
	Pi maxv = dp[N];
	while (!que.empty()) {
		int u = que.front(); que.pop();
		//cout << u << endl;
		for (auto &v : G[u]) {
			Pi nval;
			//cout << v << endl;
			nval = dp[u];
			if (prime[v]) {
				nval.first = dp[u].first+1;
				nval.second = v;
			}
			if (dp[v] < nval) {
				dp[v] = nval;
				que.push(v);
				chmax(maxv, dp[v]);
			}
		}
	}	
	if (maxv.first == 0) cout << "0 0" << endl;
	else cout << maxv.first << " " << maxv.second << endl;
	return (true);
}

void Main() {
	prime = ~prime;
	prime[0] = false;
	prime[1] = false;
	for (int i = 2; i*i <= 1000000; i++) {
		if (!prime[i]) continue;
		for (int j = i*i; j <= 1000000; j += i) {
			prime[j] = false;
		}
	}

	while (solve());
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout << fixed << setprecision(20);
	Main();
	return (0);
}



Measured memory_bytes: 108928