#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int calc(int i, int m, int n, vector< vector<int> >& a, vector<int>& X, vector<int>& Y, vector<bool>& p, vector<int>& memo) {
	if (i > m) return INT_MIN;
	if (i == n) return p[n];
	if (memo[i] < INT_MAX) return memo[i];
	int x = X[i], y = Y[i];
	int j, maxi = INT_MIN;
	j = a[y - 1][x - 1]; maxi = max(maxi, calc(j, m, n, a, X, Y, p, memo));
	j = a[y - 1][x]; maxi = max(maxi, calc(j, m, n, a, X, Y, p, memo));
	j = a[y - 1][x + 1]; maxi = max(maxi, calc(j, m, n, a, X, Y, p, memo));
	return memo[i] = maxi + p[i];
}

int main() {
	vector<bool> p(1000001, true);
	p[1] = false;
	for (int i = 2; i <= 1000000; i++)
		if (p[i])
			for (int j = i * 2; j <= 1000000; j += i)
				p[j] = false;
	vector< vector<int> > a(2000, vector<int>(2000, INT_MAX));
	vector<int> X(1000001), Y(1000001);
	int x = 1000, y = 1000, dir = 2;
	for (int i = 1; i <= 1000000; i++) {
		a[y][x] = i;
		X[i] = x; Y[i] = y;
		x += dx[dir]; y += dy[dir];
		dir = (dir + 3) % 4;
		int _x = x + dx[dir], _y = y + dy[dir];
		if (a[_y][_x] < INT_MAX) dir = (dir + 1) % 4;
	}
	for (;;) {
		int m, n; cin >> m >> n;
		if (m == 0) break;
		int maxi = 0, j = 0;
		vector<int> memo(m + 1, INT_MAX);
		for (int i = 1; i <= m; i++) {
			if (!p[i]) continue;
			int z = calc(i, m, n, a, X, Y, p, memo);
			if (z > 0 && z >= maxi) {
				maxi = z;
				j = i;
			}
		}
		cout << maxi << ' ' << j << endl;
	}
}