#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
#define MAX_N 1010010
bool prime[MAX_N];
int dp[1210][1210];
pii tmp[1210][1210];
int pos[1210][1210];
int dx[]={1,0,-1,0};
int dy[]={0,-1,0,1};
void mainmain(){
	rep(i,MAX_N){
		prime[i]=true;
	}
	prime[0]=prime[1]=false;
	for(int i=2;i*i<=MAX_N;++i){
		if(prime[i]){
			for(int j=i*i;j<MAX_N;j+=i){
				prime[j]=false;
			}
		}
	}
	int cnt=0;
	int x=601,y=600;
	pos[600][600]=1;
	dp[600][600]=0;
	reep(i,2,1000010){
		// if(i<20) cout<<y-500<<" "<<x-500<<" "<<i<<endl;
		int t=sqrt(i);
		// if(t*t==i) cout<<i<<" "<<y<<" "<<x<<endl;
		pos[y][x]=i;
		if(prime[i]) dp[y][x]=1;
		else dp[y][x]=0;
		int next=(cnt+1)%4;
		if(pos[y+dy[next]][x+dx[next]]==0){
			cnt=next;
		}
		x+=dx[cnt];
		y+=dy[cnt];
	}
	// reep(i,490,510){
		// reep(j,490,510){
			// printf("%6d ",dp[i][j]);
		// }
		// cout<<endl;
	// }

	int n,m;
	while(cin>>m>>n,n||m){
		rep(i,1210){
			rep(j,1210){
				if(pos[i][j]<=m){
					tmp[i][j].F=dp[i][j];
				}
				else tmp[i][j].F=0;
				if(tmp[i][j].F==1) tmp[i][j].S=pos[i][j];
				else tmp[i][j].S=-1;
			}
		}
		reep(i,1,1210){
			rep(j,1210){
				if(j==0){
					tmp[1210-1-i][j]=max(tmp[1210-i][0],tmp[1210-i][1]);
				}
				else if(j==1209){
					tmp[1210-1-i][j]=max(tmp[1210-i][1208],tmp[1210-i][1209]);
				}
				else tmp[1210-1-i][j]=max(max(tmp[1210-i][j-1],tmp[1210-i][j]),tmp[1210-i][j+1]);
				if(prime[pos[1210-1-i][j]]&&pos[1210-1-i][j]<=m){
					tmp[1210-1-i][j].F++;
					if(tmp[1210-1-i][j].F==1){
						tmp[1210-1-i][j].S=pos[1210-1-i][j];
					}
				}
				if(pos[i][j]==n){
					y=i;
					x=j;
				}
			}
		}
		// cout<<y<<" "<<x<<endl;
		if(tmp[y][x].S<0) cout<<0<<" "<<0<<endl;
		else cout<<tmp[y][x].F<<" "<<tmp[y][x].S<<endl;
	}
	// cout<<max(mkp(1,2),mkp(2,1)).S<<endl;
}



}
main() try{
    mainmain();
}
catch(...){
}

Measured memory_bytes: 24568