
#include<stdio.h>

#define Max 1000000
#define SqrtMax 1000

typedef struct Result {
    int count, last;
} Result;

int m, n;

int cave[SqrtMax][SqrtMax];
int itoy[Max + 1];
int itox[Max + 1];
int notPrime[Max + 1];
Result memo[SqrtMax][SqrtMax];

void makeCave() {
    int dy[4] = {0, -1, 0, 1};
    int dx[4] = {1, 0, -1, 0};
    int i;
    int y, x;
    int direction;
    y = x = SqrtMax / 2;
    direction = 0;
    cave[y][x] = 1;
    itoy[1] = y;
    itox[1] = x;
    for(i = 1; i < Max; i++) {
        y += dy[direction];
        x += dx[direction];
        cave[y][x] = i + 1;
        itoy[i + 1] = y;
        itox[i + 1] = x;
        if(!cave[y + dy[(direction + 1) % 4]][x + dx[(direction + 1) % 4]]) direction = (direction + 1) % 4;
    }
}

void makePrime() {
    int i, j;
    notPrime[0] = notPrime[1] = 1;
    for(i = 2; i <= Max; i++) {
        for(j = i << 1; j <= Max; j += i) {
            notPrime[j] = 1;
        }
    }
}

int inRange(int y, int x) {
    if(y < 0 || SqrtMax <= y) return 0;
    if(x < 0 || SqrtMax <= x) return 0;
    if(cave[y][x] > m) return 0;
    return 1;
}

Result dfs(int y, int x) {
    int dy[3] = {1, 1, 1};
    int dx[3] = {-1, 0, 1};
    int i;
    Result result;
    if(memo[y][x].count != -1) return memo[y][x];
    if(!notPrime[cave[y][x]]) {
        result.count = 1;
        result.last = cave[y][x];
    } else {
        result.count = 0;
        result.last = 0;
    }
    for(i = 0; i < 3; i++) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        Result r;
        if(!inRange(ny, nx)) continue;
        r = dfs(ny, nx);
        if(result.count > !notPrime[cave[y][x]] + r.count) {
            continue;
        } else if(result.count == !notPrime[cave[y][x]] + r.count) {
            result.last = (result.last > r.last ? result.last : r.last);
        } else {
            result.count = !notPrime[cave[y][x]] + r.count;
            result.last = r.last;
        }
    }
    return memo[y][x] = result;
}

int main(void) {
    makeCave();
    makePrime();
    while(scanf("%d %d", &m, &n), m | n) {
        int i, j;
        for(i = 0; i < SqrtMax; i++) for(j = 0; j < SqrtMax; j++) {
            memo[i][j].count = -1;
            memo[i][j].last = 0;
        }
        Result answer = dfs(itoy[n], itox[n]);
        printf("%d %d\n", answer.count, answer.last);
    }
    return 0;
}