#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()
#define FOR(i, m, n) for (int i = (m); i < (n); i++)

using ll = long long;
typedef pair<int, int> pii;
pii dp[1200][1200];
int A[1200][1200];

bool isprime[2000020];
vector<int> sosuu;
void Eratosthenes() {
    ll N = 2000020;
    int c = 0;
    REP(i, N) { isprime[i] = true; }
    for (ll i = 2; i < sqrt(N); i++) {
        if (isprime[i]) {
            for (ll j = 0; i * (j + 2) < N; j++) {
                isprime[i * (j + 2)] = 0;
            }
        }
    }
    isprime[0] = false;
    isprime[1] = false;
    REP(i, N) if (isprime[i]) sosuu.push_back(i);
    // for(ll i=2;i<N;i++){

    // }
}

void init(int M) {
    REP(i, 1200) REP(j, 1200) {
        A[i][j] = 0;
        dp[i][j] = {0, 0};
    }
    A[600][600] = 1;
    int dir = 1;
    int stride = 1;
    int x = 600, y = 600;
    int cur = 1;
    while (cur <= M) {
        REP(__, 2) {
            REP(_, stride) {
                if (dir % 4 == 0)
                    x++;
                else if (dir % 4 == 1)
                    y++;
                else if (dir % 4 == 2)
                    x--;
                else
                    y--;
                cur++;
                if (cur > M) break;
                A[x][y] = cur;
            }
            dir++;
        }
        stride++;
    }
}
const bool debug = false;
void solve() {
    int M, N;
    cin >> M >> N;
    if (M == 0) exit(0);
    init(M);
    if (debug) {
        FOR(i, 550, 650) {
            FOR(j, 550, 650) {
                cout << A[i][j] << " ";
                string s = to_string(A[i][j]);
                int rem = 4 - s.size();
                REP(_, rem) cout << " ";
            }
            cout << endl;
        }
    }

    REP(i, 1199) REP(j, 1199) {
        if (A[i][j] == 0) continue;
        if (isprime[A[i][j]]) dp[i][j].first = 0;
    }

    for (int i = 1199; i >= 0; i--) {
        REP(j, 1199) {
            if (A[i][j] == 0) continue;
            if (dp[i][j].second == 0 && isprime[A[i][j]]) {
                dp[i][j].second = A[i][j];
            }
            if (i == 0) continue;
            int tmp = 0;
            if (isprime[A[i][j]]) dp[i][j].first++;
            pii nxt = {dp[i][j].first + tmp, dp[i][j].second};

            if (j > 0 && A[i - 1][j - 1] != 0 && dp[i - 1][j - 1] < nxt) {
                dp[i - 1][j - 1] = nxt;
            }

            nxt = {dp[i][j].first + tmp, dp[i][j].second};
            if (A[i - 1][j] != 0 && dp[i - 1][j] < nxt) {
                dp[i - 1][j] = nxt;
            }

            nxt = {dp[i][j].first + tmp, dp[i][j].second};
            if (A[i - 1][j + 1] != 0 && dp[i - 1][j + 1] < nxt) {
                dp[i - 1][j + 1] = nxt;
            }
        }
    }
    REP(i, 1199) {
        REP(j, 1199) {
            if (A[i][j] == N) {
                if (dp[i][j].first == 0)
                    cout << "0 0" << endl;
                else
                    cout << dp[i][j].first << " " << dp[i][j].second << endl;
                return;
            }
        }
    }
}

signed main() {
    Eratosthenes();
    // for (auto i : sosuu) cout << i << endl;
    while (true) solve();
}


Measured memory_bytes: 22492