#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <algorithm>
#define DEBUG 0

using namespace std;

const int N=1000000;

int field[1200][1200];
pair<int,int> dp[1200][1200];

int main(void){
  vector<bool> p(N+1,true);
  p[0]=p[1]=false;
  for(int i=2;i*i<=N;++i){
    if(!p[i])continue;
    for(int j=i;i*j<=N;++j){
      p[i*j]=false;
    }
  }
  int m,n;
  while(cin>>m>>n,m||n){
    int x=600,y=600,count=1;
    int minx=1100,maxx=100,miny=1100,maxy=100;
    int startx=600,starty=600;
		for(int i=0;i<1200;++i){
			for(int j=0;j<1200;++j){
				field[i][j]=0;
			}
		}
    field[y][x]=1;
    for(int i=2;i<=m;){
      int DX[]={1,0,-1,0};
      int DY[]={0,-1,0,1};
      int d=0;
      for(int j=i+count;i<=m&&i<j;++i){
        x+=DX[d];y+=DY[d];
        maxx=max(x,maxx);maxy=max(y,maxy);
        minx=min(x,minx);miny=min(y,miny);
        if(i==n){startx=x;starty=y;}
        field[y][x]=i;
      }
      ++d;
      for(int j=i+count;i<=m&&i<j;++i){
        x+=DX[d];y+=DY[d];
        maxx=max(x,maxx);maxy=max(y,maxy);
        minx=min(x,minx);miny=min(y,miny);
        if(i==n){startx=x;starty=y;}
        field[y][x]=i;
      }
      ++d;
      ++count;
      for(int j=i+count;i<=m&&i<j;++i){
        x+=DX[d];y+=DY[d];
        maxx=max(x,maxx);maxy=max(y,maxy);
        minx=min(x,minx);miny=min(y,miny);
        if(i==n){startx=x;starty=y;}
        field[y][x]=i;
      }
      ++d;
      for(int j=i+count;i<=m&&i<j;++i){
        x+=DX[d];y+=DY[d];
        maxx=max(x,maxx);maxy=max(y,maxy);
        minx=min(x,minx);miny=min(y,miny);
        if(i==n){startx=x;starty=y;}
        field[y][x]=i;
      }
      ++d;
      ++count;
    }
#if DEBUG
    for(int i=590;i<=610;++i){
      for(int j=590;j<=610;++j){
        fprintf(stderr,"%3x",field[i][j]);
      }
      cerr<<endl;
    }
#endif
		for(int i=0;i<1200;++i){
			for(int j=0;j<1200;++j){
				dp[i][j].first=0;
				dp[i][j].second=0;
			}
		}
		
		int ansc=0,ansp=0;
		dp[starty][startx].first=(p[field[starty][startx]])?1:0;
		dp[starty][startx].second=(p[field[starty][startx]])?field[starty][startx]:0;
		ansc=dp[starty][startx].first;
		ansp=dp[starty][startx].second;
		for(int i=starty;i<maxy;++i){
			for(int j=max(minx,startx-(i-starty));j<=min(maxx,startx+(i-starty));++j){
				//cerr<<dp[i][j].first;
				for(int k=-1;k<=1;++k){
					if(j+k<minx || maxx<j+k) continue;
					int p1=field[i+1][j+k];
					if(dp[i+1][j+k].first<=(dp[i][j].first+((p[p1])?1:0))){
						if(dp[i+1][j+k].first==(dp[i][j].first+((p[p1])?1:0))){
							dp[i+1][j+k].first=dp[i][j].first+((p[p1])?1:0);
							if(dp[i+1][j+k].second<((p[p1])?p1:dp[i][j].second))
								dp[i+1][j+k].second=(p[p1])?p1:dp[i][j].second;
						}else{
							dp[i+1][j+k].first=dp[i][j].first+((p[p1])?1:0);
							dp[i+1][j+k].second=(p[p1])?p1:dp[i][j].second;
						}
						
						if(dp[i+1][j+k].first && ansc<=dp[i+1][j+k].first){
							if(ansc==dp[i+1][j+k].first){
								if(ansp<dp[i+1][j+k].second) ansp=dp[i+1][j+k].second;
								//cerr<<"#"<<field[i+1][j+k]<<" "<<dp[i+1][j+k].second<<" "<<dp[i+1][j+k].first<<" ";
							}else{
								ansc=dp[i+1][j+k].first;
								ansp=dp[i+1][j+k].second;
							}
						}
					}
				}
			}
			//cerr<<endl;
		}
		cout<<ansc<<" "<<ansp<<endl;
  }
}

Measured memory_bytes: 18200