#include <iostream>
#define REP(i, a, n) for(int i = (a); i <= (n); i++)
#define M 1000001
using namespace std;
typedef pair<int, int> pii;

bool p[M];
int m, n, table[1000][1000], nx[M], ny[M], dp1[M], dp2[M];

int isin(int y, int x) {
  return 0 <= x && x < 1000 && 0 <= y && y < 1000;
}

pii dfs(int m, int n) {
  if(dp1[n] >= 0) return pii(dp1[n], dp2[n]);

  pii ret = pii(0, p[n] ? n : 0);
  REP(i, -1, 1) {
    int qy = ny[n] + 1, qx = nx[n] + i;
    if(!isin(qy, qx)) continue;
    int q = table[qy][qx];
    if(q <= m) {
      pii t = dfs(m, q);
      ret = max(ret, t);
    }
  }

  dp1[n] = ret.first + p[n];
  dp2[n] = ret.second;
  return pii(dp1[n], dp2[n]);
}

int main(void) {
  REP(i, 1, 1000000) p[i] = true;
  p[1] = p[2] = false;
  REP(i, 2, 1000000) if(p[i]) for(int j = i * 2; j <= 1000000; j += i) p[j] = false;

  int x = 0, y = 0, cnt = 1000 - 2, r = 1000000;
  REP(i, 0, 999) REP(j, 0, 999) table[i][j] = 0;
  while(cnt >= 0) {
    REP(j, 0, cnt) { table[y][x] = r; ny[r] = y; nx[r] = x; x++; r--; }
    REP(j, 0, cnt) { table[y][x] = r; ny[r] = y; nx[r] = x; y++; r--; }
    REP(j, 0, cnt) { table[y][x] = r; ny[r] = y; nx[r] = x; x--; r--; }
    REP(j, 0, cnt) { table[y][x] = r; ny[r] = y; nx[r] = x; y--; r--; }
    if(r < 0) cout << y << " " << x << endl;
    x++;
    y++;
    cnt -= 2;
  }

  while(cin >> m >> n, m != 0 && n != 0) {
    REP(i, 1, m) dp1[i] = dp2[i] = -1;
    pii ans = dfs(m, n);
    cout << ans.first << " " << ans.second << endl;
  }

  return 0;
}