#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
//#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
//#define EXIST(s, e) ((s).find(e)!=(s).end())
//#define SORT(c) sort(begin(c),end(c))
//#define pb emplace_back
//#define MP make_pair
//#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007

//ここから編集する
class Dijkstra {
public:
    struct edge {
        ll to, cost;
    };
    typedef pair<ll, ll> P; //firstは最短距離、secondは頂点の番号

    int V;                  //超点数
    vector<vector<edge>> G; //グラフ,G[i]はiから出る辺の集合,G[i][j]はiから出る辺のj番目の辺
    vector<ll> d;           //最短距離
    vector<int> last;

    //0origin
    Dijkstra(int N) {
        V = N;
        G = vector<vector<edge >>(N);
        d = vector<ll>(N);
        last = vector<int>(N);
    };

    void dijkstra(int s) {
        priority_queue<P, vector<P>, greater<P>> que;
        fill(d.begin(), d.end(), INFl);
        d[s] = 0;
        que.push(P(0, s));

        while (!que.empty()) {
            P p = que.top();
            que.pop();
            ll v = p.second;
            if (d[v] < p.first)
                continue;
            for (int i = 0; i < G[v].size(); i++) {
                edge e = G[v][i];
                if (d[e.to] > d[v] + e.cost && !(d[e.to] == INFl && d[v] == INFl)) {
                    d[e.to] = d[v] + e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }

    /* 頂点fromに頂点toに達するコストcostの辺を貼る */
    void addEdge(int from, int to, int cost) {
        edge e;
        e.to = to;
        e.cost = cost;
        G[from].push_back(e);
    }
};


vector<bool> prime(1000005, true);

void setEra() {
    prime[0] = prime[1] = false;
    int lim = 1000005;
    rep(i, 2, lim) {
        if (prime[i]) {
            for (int j = i * 2; j < lim; j += i) {
                prime[j] = false;
            }
        }
    }
}

vector<vector<int> > makeDou(int n) {
    vector<vector<int> > tmp(2000, vector<int>(2000));
    int h = 1000;
    int w = 1000;
    int val = 1;
    int dh[] = {0, -1, 0, 1};
    int dw[] = {1, 0, -1, 0};
    int seq = 0;
    int lim = 1;
    int turn = 0;

    while (val <= n) {
        tmp[h][w] = val;
        val++;
        seq++;
        h += dh[turn % 4];
        w += dw[turn % 4];

        if (seq == lim) {
            turn++;
            seq = 0;
            lim = turn / 2 + 1;
        }
    }

    int sw = 2000, tw = 0, sh = 2000, th = 0;
    rep(i, 0, 2000) {
        rep(j, 0, 2000) {
            if (tmp[i][j] > 0) {
                sh = min(sh, i);
                sw = min(sw, j);

                th = max(th, i + 1);
                tw = max(tw, j + 1);
            }
        }
    }
    vector<vector<int>> ret(th - sh + 1, vector<int>(tw - sw + 1));
    for (int i = sh; i < th; i++) {
        for (int j = sw; j < tw; j++) {
            ret[i - sh][j - sw] = tmp[i][j];
        }
    }

    return ret;
}

bool solve() {
    int m, n;
    cin >> m >> n;
    if (n == 0 && m == 0) return false;

    auto field = makeDou(m);
    vector<vector<int> > dp(field.size(), vector<int>(field[0].size(), -1));
    int sh, sw;
    int H = field.size();
    int W = field[0].size();
    rep(i, 0, H) {
        rep(j, 0, W) {
            if (field[i][j] == n) {
                sh = i;
                sw = j;
            }
        }
    }
    dp[sh][sw] = prime[n];


    rep(i, 0, H - 1) {
        rep(j, 0, W) {
            if (dp[i][j] != -1) {
                for (int nj = j - 1; nj <= j + 1; nj++) {
                    if (nj >= 0 && nj < W) {
                        int tmp = (prime[field[i + 1][nj]]) ? dp[i][j] + 1 : dp[i][j];
                        dp[i + 1][nj] = max(dp[i + 1][nj], tmp);
                    }
                }
            }
        }
    }

    int max_v = 0;
    int max_p = 0;

    rep(i, 0, H) {
        rep(j, 0, W) {
            if (dp[i][j] > max_v) {
                max_v = dp[i][j];
            }
        }
    }
    if (max_v == 0) {
        cout << 0 << " " << 0 << endl;
        return true;
    }

    rep(i, 0, H) {
        rep(j, 0, W) {
            if (dp[i][j] == max_v && prime[field[i][j]]) {
                max_p = max(field[i][j],max_p);
            }
        }
    }

    cout << max_v << " " << max_p << endl;


    return true;

}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    if (false) {
        auto hya = makeDou(1097);
        rep(i, 0, hya.size()) {
            rep(j, 0, hya[i].size()) {
//                cout << hya[i][j] << " ";
                printf("%4d ", hya[i][j]);
            }
            cout << endl;
        }
        return 0;
    }

    setEra();
    while (solve()) {

    }

    return 0;
}



Measured memory_bytes: 22508