#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef vector<ll> vl;

ll m,n;
ll dp[1005][1005];
ll dp1[1005][1005];
ll dp2[1005][1005];
ll vis[1005][1005];
ll p[1000001];
ll si = 502,sj = 502;
ll dx[] = {0, 1, 0, -1};
ll dy[] = {1, 0, -1, 0};
ll sx,sy;
ll idx[1005][1005];


void dfs(ll x, ll y){
  if(vis[y][x]) return;
  // cerr << x << " " << y << endl;

  vis[y][x] = 1;
  dp1[y][x] = 0;
  dp2[y][x] = 0;
  vl ret = vl({0, 0});
  for(ll i = -1;i <= 1;i++){
    if(idx[y+1][x+i]){
      dfs(x+i,y+1);
      ret = max(ret,vl({dp1[y+1][x+i],dp2[y+1][x+i]}));
    }
  }
  if(dp[y][x] == 1){
    if(ret[0] == 0) ret[1] = idx[y][x];
    ret[0] += 1;

  }
  dp1[y][x] = ret[0];
  dp2[y][x] = ret[1];
  return;
}

void solve(ll m,ll n){
  ll x = si;
  ll y = sj;
  for(ll i = 0;i < 1005;i++){
    for(ll j = 0;j < 1005;j++){
      dp[i][j] = -1;
      dp1[i][j] = 0;
      dp2[i][j] = 0;
      vis[i][j] = 0;
      idx[i][j] = 0;
    }
  }
  ll d = 0;

  // cerr << m << endl;
  for(ll i = 1;i <= m;i++){
    if(i == n){
      sx = x;
      sy = y;
    }
    idx[y][x] = i;
    idx[y][x] = i;
    if(p[i]) dp[y][x] = 0;
    else dp[y][x] = 1;
    if(dp[y+dy[(d+1)%4]][x+dx[(d+1)%4]] < 0) d = (d+1)%4;
    x += dx[d];
    y += dy[d];
  }
  // for(ll i = si - 5;i <= si+5;i++){
  //   for(ll j = sj - 5;j <= sj+5;j++){
  //     cerr << dp[i][j] << " ";
  //   }
  //   cerr << endl;
  // }
  dfs(sx,sy);
  cout << dp1[sy][sx] << " " << dp2[sy][sx]<< endl;
  // for(ll i = si - 5;i <= si+5;i++){
  //   for(ll j = sj - 5;j <= sj+5;j++){
  //     cerr << dp[i][j] << " ";
  //   }
  //   cerr << endl;
  // }
  // cerr << endl;
  // for(ll i = si - 5;i <= si+5;i++){
  //   for(ll j = sj - 5;j <= sj+5;j++){
  //     cerr << dp1[i][j] << " ";
  //   }
  //   cerr << endl;
  // }

}

int main(){
  p[0] = p[1] = 1;
  for(ll i = 2;i <= 1000000;i++){
    if(p[i] == 0){
      for(ll j = 2;j*i <= 1000000;j++){
        p[i*j] = 1;
      }
    }
  }
  while(cin >> m >> n){
    if(m == 0 && n == 0) return 0;
    solve(m,n);

  }
}

