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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

vector<bool> Prime;
void initPrime(LL num) {
	Prime = vector<bool>(num + 1, true);
	Prime[1] = Prime[0] = false;
	LL sq_num = sqrt((double)num);
	for (LL i = 2; i <= sq_num; i++) {
		if (Prime[i]) {
			for (LL j = i + i; j <= num; j += i) {
				Prime[j] = false;
			}
		}
	}
}

int M, N;

constexpr int MAX_M = 1000000;
constexpr int OFFSET = 500;
constexpr int HW = 1010;

VVPII memos;
VVI ts;

void make_ts() {
    ts = VVI(HW, VI(HW));

    int y = OFFSET, x = OFFSET;
    int m = 1;
    int di = 0;

    ts[y][x] = m++;

    while (true) {
        y += DY[di];
        x += DX[di];
        ts[y][x] = m;
        if (++m > MAX_M) return;

        int ndi = (di + 1) % 4;
        int ny = y + DY[ndi];
        int nx = x + DX[ndi];
        if (ts[ny][nx] == 0) {
            di = ndi;
        }
    }
}

PII find_p(int n) {
    rep(y, HW) rep(x, HW) {
        if (ts[y][x] == n) return MP(y, x);
    }
    return MP(-1, -1);
}

PII dfs(int y, int x) {
//    printf("%d %d\n", y, x);
    if (memos[y][x].fst != -1) {
        return memos[y][x];
    }

    int t = ts[y][x];

    PII res = MP(0, Prime[t] ? t : 0);
    for (int i = -1; i <= 1; i++) {
        int ny = y + 1;
        int nx = x + i;
        int nt = ts[ny][nx];

        if (nt == 0 || nt > M) continue;

        PII tmp = dfs(ny, nx);
        res = max(res, tmp);
    }

    return memos[y][x] = MP(res.fst + Prime[t], res.snd);
}

void dump() {
    rep(y, 33) {
        rep(x, 10) {
            int yy = y + OFFSET - 17;
            int xx = x + OFFSET - 17;
            int t = ts[yy][xx];
//            printf("%5d", t);
//            cout << (Prime[t] ? '@' : ' ');
            PII p = memos[yy][xx];
            printf("(%2d %4d %4d) ", p.fst, p.snd, t);
        }
        cout << endl;
    }
}

int main(void) {
    initPrime(MAX_M);
    make_ts();

    while (cin >> M >> N, N) {
        memos = VVPII(HW, VPII(HW, MP(-1, -1)));
        PII p = find_p(N);
        PII ans = dfs(p.fst, p.snd);
        cout << ans.fst << " " << ans.snd << endl;
    }

	return 0;
}

Measured memory_bytes: 21408