#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <cmath>

const int MAXN = 1000010;
//const int MAXN = 50;
const int WH = 2*sqrt(MAXN) + 2;
using namespace std;

vector<bool> sieve(const int M){
    vector<bool> is_prime(M);
    for(int i=2;i<M;i++) is_prime[i] = true;
    for(int i=2;i*i<M;i++){
        if(not is_prime[i]) continue;
        for(int j=i*i;j<M;j+=i){
            is_prime[j] = false;
        }
    }
    return is_prime;
}

const vector<bool> is_prime = sieve(MAXN);

// N,last
pair<int,int> solve(const vector<vector<int>>& field,int m,
                    int cy,int cx,vector<vector<pair<int,int>>>& memo){
    if(memo[cy][cx].first != -1){
        return memo[cy][cx];
    }
    pair<int,int> ret = make_pair(-1,-1);
    for(int i=-1;i<=1;i++){
        int wy = cy + 1;
        int wx = cx + i;
        if(field[wy][wx] > m) continue;
        pair<int,int> p = solve(field,m,wy,wx,memo);
        ret = max(ret,p);
    }
    if(ret.first == -1){
        if(is_prime[field[cy][cx]]){
            return memo[cy][cx] = make_pair(1,field[cy][cx]);
        }else{
            return memo[cy][cx] = make_pair(0,0);
        }

    }else{
        if(ret.first == 0){
            if(is_prime[field[cy][cx]]){
                return memo[cy][cx] = make_pair(1,field[cy][cx]);
            }else{
                return memo[cy][cx] = make_pair(0,0);
            }
        }else{
            if(is_prime[field[cy][cx]]){
                return memo[cy][cx] = make_pair(ret.first+1,ret.second);
            }else{
                return memo[cy][cx] = ret;
            }
        }
    }
}

int main(){
    vector<vector<int>> field(WH,vector<int>(WH,MAXN + 100));
//    field[cy+0][cx+1] = 2;
//    field[cy-1][cx+1] = 3;
//    field[cy-1][cx+0] = 4;
//    field[cy-1][cx-1] = 5;
//    field[cy+0][cx-1] = 6;
//    field[cy+1][cx-1] = 7;
//    field[cy+1][cx+0] = 8;
//    field[cy+1][cx+1] = 9;
    {
        int cx=WH/2,cy=WH/2;
        field[cy][cx] = 1;
        int c = 2;
        for(int i=0;;i++){
            int sx = cx + i+1;
            int sy = cy + i;
            for(int j=0;j<2*i+2;j++){
                field[sy-j][sx] = c;
                c++;
                if(c >= MAXN) goto end;
            }
            vector<int> dx = {-1,0,1};
            vector<int> dy = {0,1,0};
            // left
            int ny = sy-2*i-1;
            int nx = sx;
            for(int k=0;k<3;k++){
                for(int j=0;j<2*(i+1);j++){
                    ny = ny + dy[k];
                    nx = nx + dx[k];
                    field[ny][nx] = c;
                    c++;
                    //cerr << c << " " << hy << " " << hy << endl;
                    if(c >= MAXN) goto end;
                }
            }
        }
end:;
    }
//    for(int i=0;i<WH;i++){
//        for(int j=0;j<WH;j++){
//            cout << setw(3) << field[i][j] << " ";
//        }
//        cout << endl;
//    }
//    for(int i=0;i<MAXN;i++){
//        cout << i << " " << is_prime[i] << endl;
//    }
//
    while(true){
        int m,n;
        cin >> m >> n;
        if(m == 0 and n == 0) break;
        vector<vector<pair<int,int>>> memo(WH,vector<pair<int,int>>(WH,make_pair(-1,-1)));
        int sx = -1,sy = -1;
        for(int i=0;i<WH;i++){
            for(int j=0;j<WH;j++){
                if(field[i][j] == n){
                    sy = i;
                    sx = j;
                }
            }
        }
        pair<int,int> ret = solve(field,m,sy,sx,memo);
        cout << ret.first << " " << ret.second << endl;
    }
    return 0;
}

Measured memory_bytes: 48336