//#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

int dx[] = {-1, 0, 1}, dy[] = {1, 0, -1, 0};

int memo[1100][1100];
bool prime[1000010];
PII dp[1100][1100];
signed main(void)
{

  /*REP(i, 1000) {
    REP(j, 1000) {
      cout << memo[i][j] << " ";
    }
    cout << endl;
  }*/
  //cout << memo[505][505] << endl;

  // テァツエツ?ヲツ閉ーテ」ツ?ェテ」ツつ液rue
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; i * i <= 1000010; i++) {
  	if (prime[i]) {
  		for (int j = 2 * i; j <= 1000010; j += i) {
  			prime[j] = false;
  		}
  	}
  }

  while(true) {
    int m, n;
    cin >> m >> n;
    if(!m && !n) break;

    int y = 505, x = 505, num = 1, len = 1;
    bool end = false;
    while(true) {
      REP(j, len) {
        if(num > m) {end = true; break;}
        memo[y][x] = num;
        x++;
        num++;
      }
      if(end) break;
      REP(j, len) {
        if(num > m) {end = true; break;}
        memo[y][x] = num;
        y--;
        num++;
      }
      if(end) break;
      len++;
      REP(j, len) {
        if(num > m) {end = true; break;}
        memo[y][x] = num;
        x--;
        num++;
      }
      if(end) break;
      REP(j, len) {
        if(num > m) {end = true; break;}
        memo[y][x] = num;
        y++;
        num++;
      }
      if(end) break;
      len++;
    }

    int sy, sx;
    REP(i, 1010) REP(j, 1010) {
      if(memo[i][j] == n) sy = i, sx = j;
      dp[i][j] = {-INF, INF};
    }
    //cout << "sx" << sx << " " << sy << " " << memo[sy][sx] << endl;
    queue<PII> q;
    q.push({sx, sy});
    if(prime[memo[sy][sx]]) dp[sy][sx] = {1, memo[sy][sx]};
    else dp[sy][sx] = {0, 0};
    while(q.size()) {
      PII p = q.front(); q.pop();
      int X = p.first, Y = p.second;
      //cout << y << " " << x << endl;
      REP(i, 3) {
        int ny = Y+1, nx = X + dx[i];
        //cout << memo[ny][nx] << endl;
        if(IN(0, 1100, ny) && IN(0, 1100, nx) && 1 <= memo[ny][nx] && memo[ny][nx] <= m) {
          if(prime[memo[ny][nx]]) {
            if(dp[Y][X].first >= dp[ny][nx].first) {
              //cout << ny << ":" << nx << ":" << memo[ny][nx] << endl;
              dp[ny][nx].first = dp[Y][X].first + 1;
              dp[ny][nx].second = memo[ny][nx];
              q.push({nx, ny});
            }
          } else {
            if(dp[Y][X].first > dp[ny][nx].first) {
              //cout << ny << " " << nx << " " << memo[ny][nx] << endl;
              dp[ny][nx] = dp[Y][X];
              q.push({nx, ny});
            }
          }
        }
      }
    }

    /*REP(i, 1010) {
      REP(j, 1010) {
        cout << dp[i][j].first << " ";
      }
      cout << endl;
    }*/

    int ret = -INF, idx = INF;
    REP(i, 1100) REP(j, 1100) {
      if(dp[i][j].first == INF) continue;
      if(dp[i][j].first > ret) {
        ret = dp[i][j].first;
        idx = dp[i][j].second;
        //cout << i << " " << j << " " << ret << " " << idx << endl;
      } else if(dp[i][j].first == ret && dp[i][j].second > idx) {
        ret = dp[i][j].first;
        idx = dp[i][j].second;
        //cout << i << " " << j << " " << ret << " " << idx << endl;
      }
    }
    cout << ret << " " << idx << endl;
  }

  return 0;
}

Measured memory_bytes: 17016