#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,-1,0,1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
const ll mod = 1e9 + 7;
//const int mod = 998244353;

int prime[1010101];
bool isprime[1010101];

int sieve(int n){
    int p = 0;
    for(int i=0; i<=n; i++) isprime[i] = true;
    isprime[0] = isprime[1] = false;
    for(int i=2; i<=n; i++){
        if(isprime[i]){
            prime[p++] = i;
            for(int j=2*i; j<=n; j+=i) isprime[j] = false;
        }
    }
    return p;
}

int hora[1010][1010];
pii pos[1010101];
int dp[1010][1010];

int main(){
    sieve(1000000);
    int y = 501, x = 501;
    int now = 1;
    int add = 1;
    int d = 0;
    while(now <= 1000000){
        rep(i,add){
            hora[y][x] = now;
            pos[now] = pii(y,x);
            now++;
            y += dy[d];
            x += dx[d];
        }
        if(d & 1) add++;
        d = (d + 1) % 4;
    }
    while(1){
        int n,m; cin >> n >> m;
        if(n+m == 0) break;
        pii cur = pos[m];
        rep(i,1001) rep(j,1001) dp[i][j] = -1;
        dp[cur.first][cur.second] = isprime[m];
        REP(i,cur.first+1,1001){
            rep(j,1001){
                if(hora[i][j] > n) continue;
                int c = isprime[hora[i][j]];
                if(j > 0 && dp[i-1][j-1] != -1){
                    chmax(dp[i][j], dp[i-1][j-1] + c);
                }
                if(dp[i-1][j] != -1) chmax(dp[i][j], dp[i-1][j] + c);
                if(j < 999 && dp[i-1][j+1] != -1){
                    chmax(dp[i][j], dp[i-1][j+1] + c);
                } 
            }
        }
        int ans = 0, p = 0;
        rep(i,1001) rep(j,1001) chmax(ans, dp[i][j]);
        rep(i,1001) rep(j,1001){
            if(ans == dp[i][j] && isprime[hora[i][j]]) chmax(p, hora[i][j]);
        }
        if(ans == 0) puts("0 0");
        else print2(ans, p);
    }
}


Measured memory_bytes: 20080