#include <bits/stdc++.h>

using namespace std;
const int INF = 1e9;

bool is_prime[1000100];

int sieve()
{
    int p = 0;
    for (int i = 0; i < 1000100; i++)
        is_prime[i] = true;
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i < 1000100; i++)
    {
        if (is_prime[i])
        {
            for (int j = i * 2; j < 1000100; j += i)
                is_prime[j] = false;
        }
    }
}

int rec(int num, vector<int> &dp, vector<int> &dplast, vector<vector<int>> &G)
{
    if (dp[num] != -1)
        return dp[num];
    int ret = 0;
    for (auto x : G[num])
    {
        ret = max(ret, rec(x, dp, dplast, G));
    }
    for (auto x : G[num])
    {
        if (dp[x] == ret)
            dplast[num] = max(dplast[num], dplast[x]);
    }
    if (dplast[num] == 0 && is_prime[num])
        dplast[num] = num;
    return dp[num] = ret + is_prime[num];
}

int main()
{
    vector<vector<int>> caves(1001, vector<int>(1001));
    int n, m;
    sieve();
    while (cin >> m >> n, n | m)
    {
        vector<int> dp(m + 1, -1);
        vector<int> dplast(m + 1, 0);
        vector<vector<int>> G(m + 1, vector<int>());

        for (int i = 0; i < 1001; i++)
        {
            for (int j = 0; j < 1001; j++)
            {
                caves[i][j] = INF;
            }
        }
        int step = 1, code = 1, x = 500, y = 500, num = 1;
        caves[y][x] = num++;
        while (1)
        {
            for (int i = 0; i < step; i++)
            {
                x += code;
                caves[y][x] = num++;
                if (num > m)
                    break;
            }
            if (num > m)
                break;
            for (int i = 0; i < step; i++)
            {
                y += code;
                caves[y][x] = num++;
                if (num > m)
                    break;
            }
            if (num > m)
                break;
            step++;
            code *= -1;
        }

        step = 1, code = 1, x = 500, y = 500;
        if (y - 1 > 0 && caves[y - 1][x] != INF)
            G[caves[y][x]]
                .push_back(caves[y - 1][x]);
        if (y - 1 > 0 && x - 1 > 0 && caves[y - 1][x - 1] != INF)
            G[caves[y][x]].push_back(caves[y - 1][x - 1]);
        if (y - 1 > 0 && x + 1 < 1000 && caves[y - 1][x + 1] != INF)
            G[caves[y][x]].push_back(caves[y - 1][x + 1]);
        while (1)
        {
            for (int i = 0; i < step; i++)
            {
                x += code;
                if (caves[y][x] == INF)
                    break;
                if (y - 1 > 0 && caves[y - 1][x] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x]);
                if (y - 1 > 0 && x - 1 > 0 && caves[y - 1][x - 1] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x - 1]);
                if (y - 1 > 0 && x + 1 < 1000 && caves[y - 1][x + 1] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x + 1]);
            }
            if (caves[y][x] == INF)
                break;
            for (int i = 0; i < step; i++)
            {
                y += code;
                if (caves[y][x] == INF)
                    break;
                if (y - 1 > 0 && caves[y - 1][x] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x]);
                if (y - 1 > 0 && x - 1 > 0 && caves[y - 1][x - 1] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x - 1]);
                if (y - 1 > 0 && x + 1 < 1000 && caves[y - 1][x + 1] != INF)
                    G[caves[y][x]].push_back(caves[y - 1][x + 1]);
            }
            if (caves[y][x] == INF)
                break;
            step++;
            code *= -1;
        }

        rec(n, dp, dplast, G);

        int now = n;
        int last = 0;
        while (1)
        {
            int tmp = -1;
            int next = 0;
            if (is_prime[now])
                last = now;
            if (G[now].size() == 0)
                break;
            for (auto x : G[now])
            {
                if (dp[x] > tmp)
                {
                    tmp = dp[x];
                    next = x;
                }
            }
            now = next;
        }
        cout << dp[n] << " " << dplast[n] << endl;
    }
    return 0;
}


Measured memory_bytes: 70400