#include<bits/stdc++.h>

using namespace std;

bool prime[1000001];
int mas[1004][1004];
pair< int, int > dp[1004][1004];
int v[1004][1004], now;
int px[1000001], py[1000001];
const int vy[] = {0, -1, 0, 1}, vx[] = {1, 0, -1, 0};
pair< int, int > result[114514];

pair< int, int > rec(int x, int y)
{
  if(mas[y][x] == 0) return (make_pair(0, 0));
  if(v[x][y] == now) return (dp[x][y]);
  pair< int, int > ret(0, 0);
  for(int nx = x - 1; nx <= x + 1; nx++) {
    ret = max(ret, rec(nx, y + 1));
  }
  if(!prime[mas[y][x]]) {
    if(ret.second == 0) ret.second = mas[y][x];
    ret.first++;
  }
  v[x][y] = now;
  return(dp[x][y] = ret);
}
int main()
{
  prime[1] = 1;
  for(int i = 2; i * i < 1000001; i++) {
    if(prime[i] == 0) {
      for(int j = i + i; j < 1000001; j += i) prime[j] = 1;
    }
  }
  vector< tuple< int, int, int > > query;
  for(int i = 0; ; i++) {
    int n, m;
    scanf("%d %d", &n, &m);
    if(n == 0) break;
    query.emplace_back(n, m, i);
  }
  sort(begin(query), end(query));

  int shake = 1, x = 501, y = 501, a = 3;
  for(auto& k : query) {
    int n, m, idx;
    tie(n, m, idx) = k;
    now = idx;
    for(int i = shake; i <= n; i++) {
      mas[y][x] = i;
      px[i] = x, py[i] = y;
      if(mas[y + vy[(a + 1) % 4]][x + vx[(a + 1) % 4]] == 0) a = (a + 1) % 4;
      y += vy[a], x += vx[a];
    }
    shake = n + 1;
    result[idx] = rec(px[m], py[m]);
  }
  for(int i = 0; i < query.size(); i++) {
    printf("%d %d\n", get< 0 >(result[i]), get< 1 >(result[i]));
  }
}