#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3
typedef pair<int,int> P;



P move(P p,int A){
  switch(A){
     case LEFT:
        p.second--;
        break;
     case RIGHT:
        p.second++;
        break;
     case UP:
        p.first--;
        break;
     case DOWN:
        p.first++;
        break;
  }
  return p;
}

int p[1011][1011];
void setnum(P pa,int *num,int m){
     if(*num<=m){
        //cout<<pa.first<<" "<<pa.second<<endl;;
        p[pa.first][pa.second]=*num;
        *num=(*num)+1;
        //cout<<"num="<<*num<<endl;
     }
}

void makerabit(int m){
     REP(i,1011){
         REP(j,1011){
             p[i][j]=0;
         }
     }
     P pa=P{501,501};
     p[pa.first][pa.second]=1;
     int move_num=2;
     int count=2;
     while(count<=m){
     //cout<<"test"<<m<<" "<<count<<endl;
        pa=move(pa,RIGHT);
        setnum(pa,&count,m);
        REP(i,move_num-1){
            pa=move(pa,UP);
            setnum(pa,&count,m);
        }
        REP(i,move_num){
            pa=move(pa,LEFT);
            setnum(pa,&count,m);
        }
        REP(i,move_num){
           pa=move(pa,DOWN);
           setnum(pa,&count,m);
        }
        REP(i,move_num){
           pa=move(pa,RIGHT);
           setnum(pa,&count,m);
        }
        move_num+=2;
     }
    /* for(int i=490;i<510;i++){
         for(int j=490;j<510;j++){
             cout<<setw(2)<<setfill('0')<<p[i][j]<<" ";
         }
         cout<<endl;
     }*/
     
}

//??¨????????????????????????
bool prime[1000001];
void era(){
   REP(i,1000001){
       prime[i]=true;
   }
   prime[0]=false;
   prime[1]=false;
   for(int i=2;i<1000001;i++){
       if(prime[i]==false)continue;
       for(int j=2;i*j<1000001;j++){
           prime[i*j]=false;
       }
   }
}
int dp[1011][1011];
bool visit[1011][1011];
int last[1011][1011];

int main(){
        int m,n;
        era();
        while(1){
                   //cout<<"ok"<<endl;
           cin>>m>>n;
           if(m==0&&n==0)break;
           makerabit(m);
           P pa;
           
           for(int i=0;i<1011;i++){
               REP(j,1011){
                   dp[i][j]=0;
                   if(p[i][j]==n){
                      pa.first=i;
                      pa.second=j;
                      if(prime[n]){
                         dp[i][j]++;
                         last[i][j]=p[i][j];
                      }
                   }
                   visit[i][j]=false;
               }
           }
           vector<P>v;
           v.push_back(pa);
           for(int i=0;i<v.size();i++){
               int fa=v[i].first;
               int se=v[i].second;
               if(p[fa][se]==0)continue;
               /*if(p[fa][se]!=0){
                   cout<<p[fa][se]<<"aa"<<dp[fa][se]<<endl;
               }*/
               for(int j=-1;j<=1;j++){
                   int nfa=fa+1;
                   int nse=se+j;
                   //cout<<"nfa"<<p[nfa][nse]<<endl;
                   if(nfa>=0&&nfa<1011&&nse>=0&&nse<1011){
                      int a=0;
                      if(prime[p[nfa][nse]])a=1;
                      int now=dp[nfa][nse];
                      int prev=dp[fa][se];
                      if(now<prev+a){
                         dp[nfa][nse]=max(dp[nfa][nse],dp[fa][se]+a);
                         last[nfa][nse]=last[fa][se];
                      }
                      if(now==prev){
                         last[nfa][nse]=max(last[nfa][nse],last[fa][se]);
                      }
                      if(prime[p[nfa][nse]])last[nfa][nse]=p[nfa][nse];
                      if(visit[nfa][nse]==false){
                         v.push_back(P(nfa,nse));
                         visit[nfa][nse]=true;
                      }
                   }
               }
           }
           int ans=0;
           int cave=-1;
           REP(i,1011){
              REP(j,1011){
                  if(ans<dp[i][j]){
                     ans=dp[i][j];
                     cave=p[i][j];
                  }else if(ans==dp[i][j]&&cave<last[i][j]){
                     cave=last[i][j];
                  }
                  //cout<<dp[i][j]<<" ";
              }
              //cout<<endl;
           }
           if(ans==0)cave=0;
           cout<<ans<<" "<<cave<<endl;
        }
        
	return(0);
}

Measured memory_bytes: 24972