#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int CAVEL = 1003;
const int NMAX = (int)1e6;

int cave[CAVEL][CAVEL];
bool prime[NMAX];

void mkCave(){
    int x, y;
    x = y = CAVEL / 2;
    int n = 1;
    FOR(i, 1, NMAX+1){
        REP(j, n) {
            cave[y][x] = i++;
            x++;
        }
        REP(j, n) {
            cave[y][x] = i++;
            y--;
        }
        n++;
        REP(j, n) {
            cave[y][x] = i++;
            x--;
        }
        REP(j, n) {
            cave[y][x] = i++;
            y++;
        }
        n++;
        i--;
    }
}

pii find_pos(const int n){
    REP(y, CAVEL)
        REP(x, CAVEL)
            if(cave[y][x] == n)
                return {x, y};
}

int main() {
    fill_n((bool *)prime, NMAX, true);
    prime[0] = prime[1] = false;
    FOR(i, 2, NMAX)
        if(prime[i])
            for (int j = i * 2; j < NMAX; j += i)
                prime[j] = false;

    mkCave();

    int table[CAVEL];
    int table_bak[CAVEL];

    int cavelim, into;
    while(cin >> cavelim >> into, cavelim|into){
        fill_n((int *)table, CAVEL, -1);
        fill_n((int *)table_bak, CAVEL, -1);

        pii p = find_pos(into);
        int X = p.first, Y = p.second;

        table[X] = 0;

        pii res = {0, 0};
        bool endflg = false;
        FOR(y, Y, CAVEL) {
            if (endflg) break;
            endflg = true;
            REP(x, CAVEL) {
                if (cave[y][x] > cavelim) {
                    continue;
                }
                endflg = false;

                if (table[x] != -1) {
//                    LOG("%d %d = %d %d\n",x,y,cave[y][x],prime[cave[y][x]]);
                    if (prime[cave[y][x]]) table[x]++;
                    FOR(d, -1, 1 + 1) {
                        if (x + d >= 0 && cave[y][x + d]) {
                            if(prime[cave[y][x]])
                                res = max(res, (pii) {table[x], cave[y][x]});
                            table_bak[x + d] = max(table[x], table_bak[x + d]);
                        }
                    }
                }
            }

            REP(x, CAVEL)
                table[x] = table_bak[x];
        }

        if(res.first == 0)
            cout << "0 0" << endl;
        else
            cout <<res.first << " " << res.second << endl;
    }

    //print prime
    /*
    REP(i, NMAX){
        cout << i<<" " << prime[i] << endl;
    }
     */

     //print cave
    /*
    FOR(y, CAVEL-5, CAVEL) {
        FOR(x, CAVEL - 5, CAVEL) {
            LOG("%d", cave[y][x]);
        }
        cout << endl;
    }
     */
}

Measured memory_bytes: 6072