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

#define dump(x) cout << (x) << '\n'
#define Int int64_t
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()

Int INF = 1e18;
int inf = 1e9;
Int mod = 1e9+7;

bool prime[1000001];
int field[1001][1001];
int dp[1001][1001];

int main() {
    vector<pair<int, int>> res;
    for (int i = 0; i <= 1e6; i++) {
        prime[i] = true;
    }
    prime[0] = prime[1] = false;
    for (int i = 2; i * i <= 1e6; i++) {
        if (prime[i]) {
            for (int j = 2; i * j <= 1e6; j++) {
                prime[i * j] = false;
            }
        }
    }
    while (true) {
        int m, n;
        cin >> m >> n;
        if (m == 0) break;
        int s = 1;
        while (s * s < m) {
            s++;
        }
        for (int i = 0; i < s; i++) {
            for (int j = 0; j < s; j++) {
                field[i][j] = 0;
            }
        }
        pair<int, int> cur = {s / 2, (s - 1) / 2};
        field[cur.fi][cur.se] = 1;
        queue<int> q;
        q.push(1);
        q.push(1);
        pair<int, int> start = cur;
        int mode = 0;
        int cnt = 2;
        int dx[] = {1, 0, -1, 0};
        int dy[] = {0, -1, 0, 1};
        while (cnt <= m) {
            for (int i = 0; i < q.front() and cnt <= m; i++) {
                cur.fi += dy[mode];
                cur.se += dx[mode];
                field[cur.fi][cur.se] = cnt;
                if (cnt == n) {
                    start = cur;
                }
                cnt++;
            }
            q.push(q.front() + 1);
            q.pop();
            mode = (mode + 1) % 4;
        }
        while (not q.empty()) q.pop();
        // for (int i = 0; i < s; i++) {
        //     for (int j = 0; j < s; j++) {
        //         printf("%5d", field[i][j]);
        //     }
        //     cout << endl;
        // }
        for (int i = 0; i < s; i++) {
            for (int j = 0; j < s; j++) {
                dp[i][j] = 0;
            }
        }
        if (prime[n]) {
            dp[start.fi][start.se] = 1;
        }
        for (int i = start.fi; i < s - 1; i++) {
            int p = i - start.fi;
            for (int j = max(0, start.se - p); j <= min(s - 1, start.se + p); j++) {
                if (0 <= j - 1) {
                    dp[i + 1][j - 1] = max(dp[i + 1][j - 1], dp[i][j] + prime[field[i + 1][j - 1]]);
                }
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + prime[field[i + 1][j]]);
                if (j + 1 < s) {
                    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + prime[field[i + 1][j + 1]]);
                }
            }
        }
        // for (int i = 0; i < s; i++) {
        //     for (int j = 0; j < s; j++) {
        //         printf("%3d", dp[i][j]);
        //     }
        //     cout << endl;
        // }
        int ma = -1;
        for (int i = start.fi; i < s; i++) {
            int p = i - start.fi;
            for (int j = max(0, start.se - p); j <= min(s - 1, start.se + p); j++) {
                if (field[i][j] == 0) continue;
                ma = max(ma, dp[i][j]);
            }
        }
        int ma2 = -1;
        for (int i = start.fi; i < s; i++) {
            int p = i - start.fi;
            for (int j = max(0, start.se - p); j <= min(s - 1, start.se + p); j++) {
                if (field[i][j] == 0) continue;
                if (ma == dp[i][j] and prime[field[i][j]]) {
                    ma2 = max(ma2, field[i][j]);
                }
            }
        }
        if (ma == 0) ma2 = 0;
        res.emplace_back(ma, ma2);
    }
    for (int i = 0; i < (int)res.size(); i++) {
        cout << res[i].fi << " " << res[i].se << endl;;
    }
    return 0;
}



Measured memory_bytes: 11976