#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<int> prime(1'000'001, true);
    prime[0] = false;
    prime[1] = false;
    for (int i = 2; i < 1'000'001; i++){
        if (prime[i]){
            for (int j = i * 2; j < 1'000'001; j += i) prime[j] = false;
        }
    }

    vector<vector<int>> maze(1000, vector<int>(1000, 0));
    int num = 1'000'000;
    int mode = 0;
    int x = 0;
    int y = 0;
    maze[x][y] = num;
    num--;
    while (num > 0){
        if (mode == 0){
            if (x < 0 || x >= 1000 || y+1 < 0 || y+1 >= 1000) {
                mode = 1;
                continue;
            }
            if (maze[x][y+1] != 0){
                mode = 1;
                continue;
            }
            maze[x][y+1] = num;
            y++;
            num--;
        }
        else if (mode == 1){
            if (x+1 < 0 || x+1 >= 1000 || y < 0 || y >= 1000) {
                mode = 2;
                continue;
            }
            if (maze[x+1][y] != 0){
                mode = 2;
                continue;
            }
            maze[x+1][y] = num;
            x++;
            num--;
        }
        else if (mode == 2){
            if (x < 0 || x >= 1000 || y-1 < 0 || y-1 >= 1000) {
                mode = 3;
                continue;
            }
            if (maze[x][y-1] != 0){
                mode = 3;
                continue;
            }
            maze[x][y-1] = num;
            y--;
            num--;
        }
        else if (mode == 3){
            if (x-1 < 0 || x-1 >= 1000 || y < 0 || y >= 1000) {
                mode = 0;
                continue;
            }
            if (maze[x-1][y] != 0){
                mode = 0;
                continue;
            }
            maze[x-1][y] = num;
            x--;
            num--;
        }
    }

    while (1){
        int m, n;
        cin >> m >> n;
        if (n == 0) break;
        vector<vector<int>> ans(1000, vector<int>(1000, 0));
        vector<vector<int>> cave(1000, vector<int>(1000, 0));
        int sx, sy;
        for (int i = 0; i < 1000; i++){
            for (int j = 0; j < 1000; j++){
                if (maze[i][j] == n){
                    sx = i;
                    sy = j;
                }
            }
        }
        vector<vector<bool>> check(1000, vector<bool>(1000, false));
        check[sx][sy] = true;
        if (prime[n]){
            ans[sx][sy] = 1;
            cave[sx][sy] = n;
        }
        for (int i = sx + 1; i < 1000; i++){
            for (int j = 0; j < 1000; j++){
                if (maze[i][j] > m) continue;
                for (int k = -1; k < 2; k++){
                    if (j+k < 0 || j+k >= 1000) continue;
                    if (check[i-1][j+k]){
                        if (ans[i][j] < ans[i-1][j+k] + prime[maze[i][j]]){
                            ans[i][j] = ans[i-1][j+k] + prime[maze[i][j]];
                            cave[i][j] = cave[i-1][j+k];
                        }
                        else if (ans[i][j] == ans[i-1][j+k] + prime[maze[i][j]]){
                            cave[i][j] = max(cave[i][j], cave[i-1][j+k]);
                        }
                        check[i][j] = true;
                    }
                }
                if (prime[maze[i][j]]) cave[i][j] = maze[i][j];
            }
        }

        int p = 0;
        int q = 0;
        for (int i = 0; i < 1000; i++){
            for (int j = 0; j < 1000; j++){
                if (check[i][j]){
                    if (p < ans[i][j]){
                        p = ans[i][j];
                        q = cave[i][j];
                    }
                    else if (p == ans[i][j] && q < cave[i][j]){
                        p = ans[i][j];
                        q = cave[i][j];
                    }
                }
            }
        }
        if (p == 0) cout << "0 0" << endl; 
        else cout << p << " " << q << endl;
    }
}


Measured memory_bytes: 18948