#include<iostream>
#include<algorithm>

using namespace std;
typedef pair<int, int> P ;
bool prime[2160000];
int val[1200][1200];
P dp[1200][1200];
int xs[] = {1, 0, -1, 0}, ys[] = {0, 1, 0, -1};


bool solve(){
  int m, n;
  cin >> m >> n;
  if(m == 0 && n == 0)return false;
  for(int i = 0;i < 1200;i++)
    for(int j = 0;j < 1200;j++)
      dp[i][j] = P(0, 0);
  for(int j = 1;j < 1100;j++){
    for(int i = 1;i < 1100;i++){
      dp[i][j] = max(dp[i][j-1], dp[i-1][j-1]);
      dp[i][j] = max(dp[i][j], dp[i+1][j-1]);
      if(prime[val[i][j]] && 1 <= val[i][j]  && val[i][j] <= m){
	if(dp[i][j].first == 0){
	  dp[i][j].second = val[i][j];
	}
	dp[i][j].first++;
      }
      if(val[i][j] == n){
	cout << dp[i][j].first << " " << dp[i][j].second << endl;
	return true;
      }
    }
  }
}

int main(){
  fill(prime, prime + 1080000, true);
  prime[0] = prime[1] = false;
  for(int i = 2;i  < 1080000;i++){
    if(prime[i]){
      for(long long j = (long long)i * i;j < 1080000;j += i)
	prime[j] = false;
    }
  }
  for(int i = 0, p = 1, x = 550, y = 550;i < 2000;i++){
    if(p >= 1080000)break;
    for(int j = 0;j < (i / 2) + 1;j++){
      if(p >= 1080000)break;
      val[x][y] = p++;
      x += xs[i % 4];
      y += ys[i % 4];
    }
  }
  while(solve());
  return 0;
}