#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using PLL = pair<ll, ll>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
#define ALL(V) (V).begin(), (V).end()
#define endl '\n'
    
V<PLL> dir = {
    PLL(0, 1),
    PLL(-1, 0),
    PLL(0, -1),
    PLL(1, 0),
};

V<ll> primes;

PLL advance(ll h, ll w, ll idx) {
    ll dh, dw;
    tie(dh, dw) = dir[idx];
    ll nh = h + dh, nw = w + dw;
    return make_pair(nh, nw);
}

void decide_dir(ll h, ll w, ll &idx, const VV<ll> &holes) {
    ll next_d = (idx + 1) % 4;
    ll nh, nw;
    tie(nh, nw) = advance(h, w, next_d);
    if(holes[nh][nw] == -1) idx = next_d;
}

bool is_prime(ll p) {
    auto ite = lower_bound(ALL(primes), p);
    return (ite != primes.end() && (*ite) == p);
}

ll H = 2e3 + 10, W = 2e3 + 10;

PLL dfs(ll h, ll w, const VV<ll> &holes, VV<PLL> &memo) {
    if(holes[h][w] == -1) return PLL(0, 0);
    if(memo[h][w] != PLL(-1, -1)) return memo[h][w];
    PLL ret(0, 0);
    V<PLL> recs;
    for(ll i = -1; i <= 1; i++) {
        ll nh = h + 1;
        ll nw = w + i;
        if(!(0 <= nh && nh < H &&
             0 <= nw && nw < W)) continue;
        recs.push_back(dfs(nh, nw, holes, memo));
    }
    sort(ALL(recs), 
         [](PLL a, PLL b) {
             ll ca, pa, cb, pb;
             tie(ca, pa) = a;
             tie(cb, pb) = b;
             if(ca != cb) return ca < cb;
             else return pa < pb;
         });
    if(recs.size()) {
        auto tmp = recs.back();
        ret.first += tmp.first;
        ret.second = tmp.second;
    }
    ll p = holes[h][w];
    if(is_prime(p)) {
        if(ret.second == 0) ret.second = p;
        ret.first++;
    }
    return memo[h][w] = ret;
}

bool solve() {
    ll M, N;
    cin >> M >> N;
    if(!(N + M)) return false;
    VV<ll> holes(H, V<ll>(W, -1));
    VV<PLL> memo(H, V<PLL>(W, PLL(-1, -1)));
    ll didx = 3;
    ll h = holes.size() / 2, w = holes.size() / 2;
    ll init_h, init_w;
    for(ll i = 1; i <= M; i++) {
        holes[h][w] = i;
        if(i == N) tie(init_h, init_w) = PLL(h, w);
        decide_dir(h, w, didx, holes);
        tie(h, w) = advance(h, w, didx);
    }
    auto ans = dfs(init_h, init_w, holes, memo);
    cout << ans.first << ' ' << ans.second << endl;
    return true;
}

int main() {
    V<bool> used(1e6 + 10, false);
    for(ll i = 2; i < used.size(); i++) {
        if(used[i]) continue;
        primes.push_back(i);
        for(ll j = 1; i * j < used.size(); j++) used[i * j] = true;
    }
    while(solve());
    return 0;
}

