#include <bits/stdc++.h>

using namespace std;
#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define _dec(i,N,j) for(int i = (N-1);i >= (j);i--)
#define _rec(i,N,j) for(int i = (N);i >=(j);i--)
#define ALL(x) x.begin(),x.end()
#define MEM(a,n) memset(a,n,sizeof(a))
#define fst first
#define scd second
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

int get_int(){int x;scanf("%d",&x);return x;}
LL get_ll(){LL x;scanf("%lld",&x);return x;}
double get_db(){double x;scanf("%lf",&x);return x;}

template<typename T> int chmin(T &a,T &b){if(a > b){a = b;return 1;}else return 0;}
template<typename T> int chmax(T &a,T &b){if(a < b){a = b;return 1;}else return 0;}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {_for(i,0,v.size()) os << v[i] << " ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){for(auto a:v) os << a <<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& v){os << v.fst <<" "<<v.scd << " ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>& v){for(auto a:v) cout << a << endl;return os;}

static const int dx[8] = { 0,  -1, 0,  1,  1, -1,  1, -1};
static const int dy[8] = { 1,  0,  -1, 0,  1, -1, -1,  1};
const double pi  = acos(-1.0);
const double eps = 1e-8;
const LL  INF  = 0x3f3f3f3f3f3f3f3f;
const int inf  = 0x3f3f3f3f;

const int maxn = 1e6+5;
const int maxh = 1e3;
int G[2*maxh][2*maxh];
int isprime[maxn];

map<int,pii> mp;

pii dp[maxh*2][maxh*2];

int get_input(){
    return 1;
}
void output(){;}

void build(){
    _for(i,0,maxn){
        isprime[i] = 1;
    }
    isprime[1] = 0;
    _for(i,2,sqrt(maxn)+1){
        if(isprime[i]){
            for(int j = 2*i; j < maxn;j+=i){
                isprime[j] = 0;
            }
        }
    }


    int x = maxh;int y = maxh;
    MEM(G,-1);
    int step = 1;
    int num = 1;
    G[x][y] = num;
    mp[1] = MP(maxh,maxh);

    int k = 0;

    while(true){
        if(num >= 1e6) break;
        _for(i,0,4){
            k++;
            if(num >= 1e6) break;
            _for(j,0,step){
                if(num >= 1e6) break;
                num++;
                x+= dx[i];
                y += dy[i];
                G[x][y] = num;
                mp[num] = MP(x,y);
            }
            if(k == 2){
                step++;
                k = 0;
            }
        }
    }
    /*
    _for(i,maxh -10,maxh + 10) {
        _for(j,maxh-1,maxh+10)
        {
            printf("%d ",G[i][j]);
        }
        printf("\n");
    }
    */

}
int N,M;

bool valid(int x,int y){
    int n = G[x][y];
    if(n == -1 || n > M){
        return false;
    }
    return true;
}

pii searching(int x,int y){
    pii &ret = dp[x][y];
    if(ret.first >= 0) return ret;

    if(isprime[G[x][y]]){
        ret.second = G[x][y];
        ret.first = 1;
    }else{
        ret.first = 0;
    }

    _for(i,-1,2){
        int nexx = x + 1;
        int nexy = y + i;
        if(valid(nexx,nexy)){
            pii kk = searching(nexx,nexy);
            kk.first += isprime[G[x][y]];
            chmax(ret,kk);
        }
    }

    return ret;

}

int  main()
{
    build();
    while(scanf("%d %d",&M,&N)&&N){
        _for(i,0,2*maxh) _for(j,0,2*maxh){
            dp[i][j] = MP(-1,-1);
        }

        pii pos = mp[N];
        int stx = pos.first;
        int sty = pos.second;

        pii ans = searching(stx,sty);

        if(ans.first == 0) ans.second = 0;
        cout << ans.first <<" "<<ans.second<<endl;

    }
    return 0;
}



Measured memory_bytes: 116520