#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int num[1700][1700];
i_i pos[1000500];
i_i dp[1700][1700];
i_i before[1700][1700];
bool IsPrime[1000050];
void init() {
    for(int i = 0; i < 1700; i++) {
        for(int j = 0; j < 1700; j++) dp[i][j] = {0, 0};
    }
}
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    for(int i = 0; i < 1700; i++) {
        for(int j = 0; j < 1700; j++) num[i][j] = 0;
    }
    for(int i = 0; i <= 1000000; i++) pos[i] = {0, 0};
    for(int i = 2; i <= 1000000; i++) IsPrime[i] = true;
    IsPrime[0] = false;
    IsPrime[1] = false;
    for(int i = 2; i <= 1000000; i++) {
        if(!IsPrime[i]) continue;
        for(int j = i * 2; j <= 1000000; j += i) IsPrime[j] = false;
    }
    num[1000][1000] = 1;
    pos[1] = {1000, 1000};
    int nowi = 1000;
    int nowj = 1000;
    int val = 1;
    int imax = 1000;
    int imin = 1000;
    int jmax = 1000;
    int jmin = 1000;
    for(int delta = 1; delta <= 1000; delta += 2) {
        //cerr << val << endl;
        for(int k = 1; k <= delta; k++) {
            nowj++;
            val++;
            if(num[nowi][nowj] != 0) {
                //cerr << nowi << " " << nowj << " " << num[nowi][nowj] << " " << val << endl;
                return 0;
            }
            num[nowi][nowj] = val;
            pos[val] = {nowi, nowj};
            imax = max(imax, nowi);
            imin = min(imin, nowi);
            jmax = max(jmax, nowj);
            jmin = min(jmin, nowj);
        }
        for(int k = 1; k <= delta; k++) {
            nowi--;
            val++;
            if(num[nowi][nowj] != 0) {
                //cerr << nowi << " " << nowj << " " << num[nowi][nowj] << " " << val << endl;
                return 0;
            }
            num[nowi][nowj] = val;
            pos[val] = {nowi, nowj};
            imax = max(imax, nowi);
            imin = min(imin, nowi);
            jmax = max(jmax, nowj);
            jmin = min(jmin, nowj);
        }
        for(int k = 1; k <= delta + 1; k++) {
            nowj--;
            val++;
            if(num[nowi][nowj] != 0) {
                cerr << nowi << " " << nowj << " " << num[nowi][nowj] << " " << val << endl;
                return 0;
            }
            num[nowi][nowj] = val;
            pos[val] = {nowi, nowj};
            imax = max(imax, nowi);
            imin = min(imin, nowi);
            jmax = max(jmax, nowj);
            jmin = min(jmin, nowj);
        }
        for(int k = 1; k <= delta + 1; k++) {
            nowi++;
            val++;
            if(num[nowi][nowj] != 0) {
                cerr << nowi << " " << nowj << " " << num[nowi][nowj] << " " << val << endl;
                return 0;
            }
            num[nowi][nowj] = val;
            pos[val] = {nowi, nowj};
            imax = max(imax, nowi);
            imin = min(imin, nowi);
            jmax = max(jmax, nowj);
            jmin = min(jmin, nowj);
        }
    }
    //cerr << imax << " " << imin << " " << jmax << " " << jmin << endl;
    //cerr << val << endl;
    //cerr << num[516][1377] << endl;
    //if(IsPrime[936164]) cerr << "HOGE" << endl;
    //cerr << pos[613].first << " " << pos[613].second << endl;
    for(int i = 1; i <= 85; i++) {
        //cerr << i << " " << pos[i].first << " " << pos[i].second << endl;
    }
    while(true) {
        int m, n;
        cin >> m >> n;
        if(n == 0 && m == 0) break;
        init();
        for(int i = 1698; i >= 0; i--) {
            for(int j = 1; j <= 1650; j++) {
                if(num[i][j] == 0) continue;
                if(num[i][j] > m) continue;
                //cerr << i << " " << j << " " << num[i][j] << endl;
                if(IsPrime[num[i][j]]) dp[i][j] = {0, num[i][j]};
                before[i][j] = {-1, -1};
                for(int k = j - 1; k <= j + 1; k++) {
                    if(dp[i+1][k] > dp[i][j]) {
                        dp[i][j] = max(dp[i][j], dp[i+1][k]);
                        before[i][j] = {i+1, k};
                    }
                }
                if(IsPrime[num[i][j]]) dp[i][j].first++;
            }
        }
        /*
        int I = pos[n].first;
        int J = pos[n].second
        */
        i_i now = dp[pos[n].first][pos[n].second];
        //cout << dp[I][J].first << " " << dp[I][J].second << endl;
        cout << now.first << " " << now.second << endl;
        int inow = pos[n].first;
        int jnow = pos[n].second;
        while(inow != -1) {
            //cerr << inow << " " << jnow << " " << dp[inow][jnow].first << endl;
            int nexti = before[inow][jnow].first;
            jnow = before[inow][jnow].second;
            inow = nexti;
        }
    }
    return 0;
}


Measured memory_bytes: 57608