#include <cstdio>
#include <utility>
#include <algorithm>

using namespace std;

bool prime[1000001];
// [x][y]
int cave_no[1003][1003];
int dp[1003][1003];
int route[1003][1003];

void init_cave_no() {
    int i = 1;
    int n = 1;

    int x = 501;
    int y = 501;

    for (;;) {
        // right
        for (int j = n; j > 0; j--) {
            cave_no[x][y] = i++;
            ++x;
            if (i > 1000000) { goto end; }
        }
        // up
        for (int j = n; j > 0; j--) {
            cave_no[x][y] = i++;
            ++y;
            if (i > 1000000) { goto end; }
        }

        ++n;

        // left
        for (int j = n; j > 0; j--) {
            cave_no[x][y] = i++;
            --x;
            if (i > 1000000) { goto end; }
        }
        // down
        for (int j = n; j > 0; j--) {
            cave_no[x][y] = i++;
            --y;
            if (i > 1000000) { goto end; }
        }

        ++n;
    }
end:;
}

void find_cave_no(int no, int* px, int* py) {
    int i = 1;
    int n = 1;

    int x = 501;
    int y = 501;

    for (;;) {
        // right
        for (int j = n; j > 0; j--) {
            if (i == no) { goto end; }
            ++x; ++i;
        }
        // up
        for (int j = n; j > 0; j--) {
            if (i == no) { goto end; }
            ++y; ++i;
        }

        ++n;

        // left
        for (int j = n; j > 0; j--) {
            if (i == no) { goto end; }
            --x; ++i;
        }
        // down
        for (int j = n; j > 0; j--) {
            if (i == no) { goto end; }
            --y; ++i;
        }

        ++n;
    }
end:;
    *px = x; *py = y;
}

void init_prime() {
    prime[0] = false;
    prime[1] = false;

    for (int i = 2; i <= 1000000; i++) {
        prime[i] = true;
    }

    for (int i = 2; i <= 1000; i++) {
        if (prime[i]) {
            for (int j = i + i; j <= 1000000; j += i) {
                prime[j] = false;
            }
        }
    }
}

int main() {
    init_cave_no();
    init_prime();

    for (;;) {
        int m, n;
        scanf("%d%d", &m, &n);

        if (!m && !n) { break; }

        int sx, sy;
        find_cave_no(n, &sx, &sy);

        for (int i = 0; i < 1003; i++) {
            for (int j = 0; j < 1003; j++) {
                dp[i][j] = 0;
                route[i][j] = 0;
            }
        }

        // i => x-coord, j => y-coord
        for (int j = 2; j <= 1001; j++) {
            for (int i = 2; i <= 1001; i++) {
                int no = cave_no[i][j];
                if (no != 0 && no <= m) {
                    bool p = prime[no];

                    int m = dp[i - 1][j - 1];
                    int m_route = route[i - 1][j - 1];

                    if (m < dp[i][j - 1]) {
                        m = dp[i][j - 1];
                        m_route = route[i][j - 1];
                    } else if (m == dp[i][j - 1]) {
                        m_route = max(m_route, route[i][j - 1]);
                    }

                    if (m < dp[i + 1][j - 1]) {
                        m = dp[i + 1][j - 1];
                        m_route = route[i + 1][j - 1];
                    } else if (m == dp[i + 1][j - 1]) {
                        m_route = max(m_route, route[i + 1][j - 1]);
                    }

                    if (m_route == 0 && p) {
                        route[i][j] = no;
                    } else {
                        route[i][j] = m_route;
                    }
                    dp[i][j] = m + (p ? 1 : 0);
                }
            }
        }

        printf("%d %d\n", dp[sx][sy], route[sx][sy]);
    }
    return 0;
}

Measured memory_bytes: 13792