#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int fie[2000][2000];
bool isp[1000001];
int dp[2000][2000];


void makeEra(){
    for(int i=0;i<=1001;i++){
        if( isp[i] ){
            for(int j=2;i*j<1000001;j++){
                isp[i*j]=false;
            }
        }
    }
}

bool is(int n,int dir,int y,int x){
    if( dir==0 ){
        if( fie[y-1][x]==-1 )return true;
    }
    if( dir==1 ){
        if( fie[y][x-1]==-1 )return true;
    }
    if( dir==2 ){
        if( fie[y+1][x]==-1 )return true;
    }
    if( dir==3 ){
        if( fie[y][x+1]==-1 )return true;
    }
    return false;
}

void makeFie(int n){
    int y=1000,x=1000;
    int dir = 0; //テ・ツ渉ウ0 テ、ツクツ? テ・ツキツヲ2 テ、ツクツ?
    int dy[]={0,-1,0,1};
    int dx[]={1,0,-1,0};

    for(int i=1;i<=n;i++){
        fie[y][x]=i;
        y+=dy[dir];
        x+=dx[dir];
        if( is(i,dir,y,x) ) dir=(dir+1)%4;

    }
}


int main(){
    rep(i,1000001) isp[i]=true;
    isp[0]=isp[1]=false;
    makeEra();
    
    int m,n;
    while(cin>>m>>n){
        if(m==0&&n==0)break;
        rep(i,2000)rep(j,2000)fie[i][j]=-1;
        makeFie(m);

        int y,x;
        rep(i,2000)rep(j,2000){
            if(fie[i][j]==n){
                y=i;
                x=j;
                break;
            }
        }
        
        rep(i,2000)rep(j,2000)dp[i][j]=-1;
        dp[y][x]=0;
        if(isp[n])dp[y][x]++;
        
        rep(i,2000-1){
            rep(j,2000){
                if( dp[i][j]!=-1 ){
                    if( j-1>=0 && fie[i+1][j-1]!=-1 )   dp[i+1][j-1] = max( dp[i][j]+isp[ fie[i+1][j-1] ] , dp[i+1][j-1] );
                    if( fie[i+1][j]!=-1 )               dp[i+1][ j ] = max( dp[i][j]+isp[ fie[i+1][ j ] ] , dp[i+1][ j ] );
                    if( j+1<2000 && fie[i+1][j+1]!=-1)  dp[i+1][j+1] = max( dp[i][j]+isp[ fie[i+1][j+1] ] , dp[i+1][j+1] );
                }
            }
        }
        int pre=-1,maxi=-1;
        
        rep(i,2000){
            rep(j,2000){
                if( dp[i][j]>maxi || (dp[i][j]==maxi && isp[fie[i][j]] && fie[i][j]>pre) ){
                    maxi=dp[i][j];
                    pre=fie[i][j];
                }
            }
        }
        
        if( maxi==0 )cout<<"0 0"<<endl;
        else cout<<maxi<<" "<<pre<<endl;
        
    }
}


/*
bool isLeep(int n){
    return ( (n%4==0 && n%100!=0) || n%400==0 );
}


ll ymdToNum(int y,int m,int d){
    
    int month[]={0,0,31,59,90,120,151,181,212,243,273,304,334};
    
    ll ret=0;
    ret+=365*(y-1);
    ret+=(int)((y-1)/4) - (int)((y-1)/100) + (int)((y-1)/400);
    
    ret+=month[m];
    if( isLeep(y) && m>=3 )ret++;
    
    ret+=d-1;
    
    return ret;
}

ll bkatwkiToNum(int b,int ka,int t,int w,int ki){
    ll ret=0;
    ret+=b;
    ret*=20;
    
    ret+=ka;
    ret*=20;
    
    ret+=t;
    ret*=18;
    
    ret+=w;
    ret*=20;
    
    ret+=ki;
    
    return ret;
}


int main(){
    string s;
    while(cin>>s){
        if( s=="#" )break;
        
        string buf="";
        vector<string> tmp;
        rep(i,s.size()){
            if( s[i]!='.' )buf+=s[i];
            else{
                tmp.pb(buf);
                buf="";
            }
        }
        tmp.pb(buf);


        //b.ka.t.w.ki
        if( tmp.size()==5 ){ //maya
            int b  = stoi(tmp[0]);
            int ka = stoi(tmp[1]);
            int t  = stoi(tmp[2]);
            int w  = stoi(tmp[3]);
            int ki = stoi(tmp[4]);
            
            ll num = bkatwkiToNum(b,ka,t,w,ki);
            
            num += ymdToNum(2012,12,21);
            
            int y,m,d;
            int t1=0;
            for(int i=1;;i++){
                t1+=365;
                if( isLeep(i) )t1++;
                
                if(t1>num){
                    y=i;
                    break;
                }
            }
            
            num -= t1-365;
            if( isLeep(y) )num--;
            
            int month[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
            rep(i,13){
                int ep=0;
                if( isLeep(y)&&i>2 )ep++;
                
                if( month[i]+ep>num ){
                    m=i;
                    break;
                }
            }
            num -= month[m-1];
            
            d=num+1;
            
            cout<<y<<"."<<m<<"."<<d<<endl;
            
            
        }else if( tmp.size()==3 ){   //sei
            int y = stoi(tmp[0]);
            int m = stoi(tmp[1]);
            int d = stoi(tmp[2]);
            
            ll num = ymdToNum(y,m,d);
            
            cout<<"-"<<endl;
        }else return 0;
    }
}

*/

Measured memory_bytes: 33384