#include <bits/stdc++.h>
using namespace::std;
typedef long long lint;
#define rep(i,n) for(int i=0;i<n;i++)
#define repi(i,a,b) for(int i=a;i<b;i++)
#define cout (cout<<fixed<<setprecision(15))
#define INF 1LL<<40
inline set<lint> primeSet(lint n) {
    vector<bool> p(n+1);
    set<lint> set;
    repi(i,1, n+1)p[i] = true;
    repi(i,2,sqrt(n)+1) {
        if (p[i]) {
            repi(j,2,n/i+1) {
                p[i*j] = false;
            }
        }
    }
    repi(i, 2, n+1)if (p[i])set.insert(i);
    return set;
}
inline lint min(vector<lint> num){
    lint M=num[0];
    rep(i,(lint)num.size())M=min(M,num[i]);
    return M;
}
int main(){
    while(1){
        lint m,n,k=1;
        cin>>m>>n;
        if(m==0&&n==0)break;
        while(k*k<m)k+=2;
        vector<vector<lint>> map(k,vector<lint>(k,0));
        lint x=k/2,y=k/2,sx=0,sy=0;
        map[y][x]=1;
        if(1==n)sx=x,sy=y;
        x++;y++;
        lint cnt=2;
        repi(i,1,k/2+1){
            rep(j,i*2){
                y--;
                map[y][x]=cnt;
                if(cnt==n)sx=x,sy=y;
                cnt++;
            }
            rep(j,i*2){
                x--;
                map[y][x]=cnt;
                if(cnt==n)sx=x,sy=y;
                cnt++;
            }
            rep(j,i*2){
                y++;
                map[y][x]=cnt;
                if(cnt==n)sx=x,sy=y;
                cnt++;
            }
            rep(j,i*2){
                x++;
                map[y][x]=cnt;
                if(cnt==n)sx=x,sy=y;
                cnt++;
            }
            x++;y++;
        }
        //rep(i,k){cout<<endl;rep(j,k)printf("%3lld",map[i][j]);}
        vector<vector<lint>> dp,dp2;
        set<lint> p=primeSet(m);
        dp.push_back({(lint)p.count(n)});
        lint now=1;
        lint M=p.count(n);
        lint N;
        if(p.count(n))dp2.push_back({n});
        else dp2.push_back({0});
        if(p.count(n))N=n;
        else N=0;
        while(1){
            sx--;
            sy++;
            dp.push_back({});
            dp2.push_back({});
            bool f=false;
            rep(i,now*2+1){
                lint t=-1;
                lint u=0;
                if(0<=i&&i<(lint)dp[now-1].size()){
                    if(t<dp[now-1][i])u=max(u,dp2[now-1][i]);
                    t=max(t,dp[now-1][i]);
                }
                if(0<=i-1&&i-1<(lint)dp[now-1].size()){
                    if(t<dp[now-1][i-1])u=max(u,dp2[now-1][i-1]);
                    t=max(t,dp[now-1][i-1]);
                }
                if(0<=i-2&&i-2<(lint)dp[now-1].size()){
                    if(t<dp[now-1][i-2])u=max(u,dp2[now-1][i-2]);
                    t=max(t,dp[now-1][i-2]);
                }
                if(t!=-1&&0<=sy&&sy<k&&0<=sx+i&&sx+i<k&&map[sy][sx+i]<=m){
                    f=true;
                    if(p.count(map[sy][sx+i]))u=map[sy][sx+i];
                    dp2[now].push_back(u);
                    dp[now].push_back(t+p.count(map[sy][sx+i]));
                    if(p.count(map[sy][sx+i])&&M==t+(lint)p.count(map[sy][sx+i]))N=max(N,u);
                    if(M<t+(lint)p.count(map[sy][sx+i]))N=u;
                    M=max<lint>(M,t+p.count(map[sy][sx+i]));
                }else{
                    dp2[now].push_back(-1);
                    dp[now].push_back(-1);
                }
            }
            now++;
            //rep(i,dp.size()){cout<<endl;rep(j,dp[i].size())printf("%3lld",dp[i][j]);}
            if(!f){
                cout<<M<<" "<<N<<endl;
                break;
            }
        }
    }
}


Measured memory_bytes: 34552