#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 30000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
long long powering(long long now, long long now_go) {
	long long ans = 1;
	while (now_go != 0) {
		if (now_go % 2 == 1) {
			ans *= now;
			ans %= MAX_MOD;
		}
		now *= now;
		now %= MAX_MOD;
		now_go /= 2;
	}
	return ans;
}
vector<long long> calc(vector<long long> a, vector<long long> b) {
	long long k = sqrt(a.size());
	vector<long long> ans;
	for (int i = 0; i < k; ++i) {
		for (int q = 0; q < k; ++q) {
			long long geko = 0;
			for (int t = 0; t < k; ++t) {
				geko += a[i * k + t] * b[t * k + q];
				geko %= MAX_MOD;
			}
			ans.push_back(geko);
		}
	}
	return ans;
}
vector<long long> powered(vector<long long> a, long long n) {
	vector<long long> ans = a;
	n--;
	while (n != 0) {
		if (n % 2 == 1) {
			ans = calc(ans, a);
		}
		a = calc(a, a);
		n /= 2;
	}
	return ans;
}
long long inv(long long now) {
	return powering(now, MAX_MOD - 2LL);
}
unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
	unsigned long t = (x^(x << 11));
	x = y; y = z; z = w;
	return (w = (w^(w >> 19))^(t^(t >> 8)));
}
long long grid[1020][1020] = {};
pair<long long, long long> directors[2000000];
long long sosuu[2000000];
pair<long long,long long> dp[1020][1020] = {};
pair<long long, long long> solve(long long now_x, long long now_y, long long maximum) {
	if (grid[now_x][now_y] == 0 || grid[now_x][now_y] > maximum) {
		return make_pair(0, 0);
	}
	if (dp[now_x][now_y].second != 0) {
		return dp[now_x][now_y];
	}
	int ye[3] = { -1,0,1 };
	REP(i, 3) {
		int next_x = now_x + 1;
		int next_y = now_y + ye[i];
		pair<long long, long long> neko = solve(next_x, next_y, maximum);
		int ok = 0;
		if (dp[now_x][now_y].first < neko.first) {
			ok = 1;
		}
		if (dp[now_x][now_y].first == neko.first && dp[now_x][now_y].second < neko.second) {
			ok = 1;
		}
		if (ok == 1) {
			dp[now_x][now_y] = neko;
		}
	}
	if (sosuu[grid[now_x][now_y]] == 1) {
		if (dp[now_x][now_y].first == 0) {
			dp[now_x][now_y].second = grid[now_x][now_y];
		}
		dp[now_x][now_y].first++;
	}
	return dp[now_x][now_y];
}
int main(){
	long long now_x = 510, now_y = 510;
	int direction = 3;
	int xe[4] = { 0,-1,0,1 };
	int ye[4] = { 1,0,-1,0 };
	for (int i = 1; i <= 1000001; ++i) {
		if (i != 1) {
			int ok = 1;
			for (int q = 2; q <= sqrt(i); ++q) {
				if (i % q == 0) {
					ok = 0;
					break;
				}
			}
			sosuu[i] = ok;
		}
		if (grid[now_x + xe[(direction + 1) % 4]][now_y + ye[(direction + 1) % 4]] == 0) {
			direction++;
			direction %= 4;
		}
		grid[now_x][now_y] = i;
		directors[i] = make_pair(now_x, now_y);
		now_x += xe[direction];
		now_y += ye[direction];
	}
	while (true) {
		long long n, k;
		cin >> n >> k;
		if (n == 0) return 0;
		REP(i, 1020) {
			REP(q, 1020) {
				dp[i][q] = make_pair(0, 0);
			}
		}
		pair<long long, long long> a = solve(directors[k].first, directors[k].second, n);
		cout << a.first << " " << a.second << endl;
	}
}


Measured memory_bytes: 50864