import std.stdio, std.string, std.algorithm;
import std.conv, std.array;
import std.typecons;
import std.functional;

const int M = 1003;
alias Tuple!(int, int) pair;

int[M][M] F;
pair[M * M + 1] T;
int[4] dx = [1, 0, -1, 0];
int[4] dy = [0, -1, 0, 1];

bool[] prime;
void init_prime() {
    prime = new bool[M * M + 1];
    prime.fill(true);
    prime[0] = prime[1] = false;
    for (int i = 2; i <= M * M; i++) {
        if (prime[i]) {
            for (int j = i + i; j <= M * M; j += i) {
                prime[j] = false;
            }
        }
    }
}

void P() {
    foreach (xs; F) {
        foreach (x; xs) {
            writef("%3d ", x);
        }
        writeln;
    }
}

void init_F() {
    int y = M / 2, x = M / 2;
    int d = 0;
    int c = 1;
    int clim = 1;
    int used = 0;
    bool f = false;
    while (c <= M * M) {
        //stderr.writeln(y, " " , x);
        F[y][x] = c;
        T[c] = pair(y, x);
        c++;
        y += dy[d];
        x += dx[d];
        used++;
        if (used == clim) {
            d = (d + 1) % 4;
            if (f) {
                clim++;
                f = false;
            } else {
                f = true;
            }
            used = 0;
        }
    }
}

void main() {
    init_prime;
    init_F;
    int m, n;
    while (true) {
        readf("%d %d\n", &m, &n);
        if (m == 0 && n == 0) break;
        solve(m, n);
    }
}

void solve(int m, int n) {
    int y = T[n][0], x = T[n][1];
    pair ans = count(m, y, x);
    writef("%d %d\n", ans[0], ans[1]);
}

pair count(int m, int y, int x) {
    if (F[y][x] > m) return pair(0, 0);
    alias memoize!count mcount;
    pair ans =  max(mcount(m, y + 1, x - 1),
                    mcount(m, y + 1, x),
                    mcount(m, y + 1, x + 1));
    if (prime[F[y][x]]) {
        if (ans[0] == 0) {
            ans[1] = F[y][x];
        }
        ans[0]++;
    }
    return ans;
}