//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>

using namespace std;


typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef pair<int, int> PII;


/*
 *
 *
 *   ~~~~Below My Answer~~~~
 *
 *
 **/

const int MAP_SIZE = 2000;

inline int SQR(int n) {
    return n * n;
}

vector<bool> ERATOSTHENES(int n) {
    vector<bool> arr(n, true);
    for (int i = 2; SQR(i) < n; i++) {
        if (arr[i]) {
            for (int j = 0; i * (j + 2) < n; j++) {
                arr[i * (j + 2)] = false;
            }
        }
    }
    return arr;
}

void makeMap(VVI &map, VVB &used, VB &primes, PII start, int m) {
    int count = 1;
    int right = 1;
    int up = 1;
    int left = 2;
    int down = 2;
    PII point = start;

    while (true) {
        for (int i = 0; i < right; i++) {
            if (count < m) {
                count++;
                point.second += 1;
                map[point.first][point.second] = count;
                if (primes[count]) {
                    used[point.first][point.second] = true;
                }
            }
        }
        for (int i = 0; i < up; i++) {
            if (count < m) {
                count++;
                point.first -= 1;
                map[point.first][point.second] = count;
                if (primes[count]) {
                    used[point.first][point.second] = true;
                }
            }
        }
        for (int i = 0; i < left; i++) {
            if (count < m) {
                count++;
                point.second -= 1;
                map[point.first][point.second] = count;
                if (primes[count]) {
                    used[point.first][point.second] = true;
                }
            }
        }
        for (int i = 0; i < down; i++) {
            if (count < m) {
                count++;
                point.first += 1;
                map[point.first][point.second] = count;
                if (primes[count]) {
                    used[point.first][point.second] = true;
                }
            }
        }

        if (count < m) {
            right += 2;
            up += 2;
            left += 2;
            down += 2;
        } else {
            break;
        }

    }
}

int main() {

    vector<bool> primes = ERATOSTHENES(2000000);

    while (true) {
        int m, n;
        cin >> m >> n;

        if (m == 0 && n == 0) break;

        VVI map(MAP_SIZE, VI(MAP_SIZE, 0));
        VVB used(MAP_SIZE, VB(MAP_SIZE, false));
        //first = y   second = x
        PII start(MAP_SIZE / 2, MAP_SIZE / 2);
        map[start.first][start.second] = 1;

        makeMap(map, used, primes, start, m);

        for (int i = 0; i < map.size(); i++) {
            for (int j = 0; j < map[i].size(); j++) {
                if (map[i][j] == n) start = make_pair(i, j);
            }
        }

        VVI dp(MAP_SIZE, VI(MAP_SIZE, -1));
        if (used[start.first][start.second]) {
            dp[start.first][start.second] = 1;
        } else {
            dp[start.first][start.second] = 0;
        }
//        for (int j = 2; j <= MAP_SIZE - 2; j++) {
//            if (used[start.first][j])dp[start.first][j] = 1;
//        }

        for (int i = start.first + 1; i < MAP_SIZE - 2; i++) {
            for (int j = 2; j < MAP_SIZE - 2; j++) {
                if (map[i][j] == 0)continue;
                int maxV = -1;
                maxV = max(maxV, dp[i - 1][j - 1]);
                maxV = max(maxV, dp[i - 1][j]);
                maxV = max(maxV, dp[i - 1][j + 1]);
                if (maxV == -1) continue;
                if (used[i][j]) maxV++;
                dp[i][j] = maxV;
            }
        }


        int maxV = 0;
        int number = 0;
        PII last(0, 0);

        for (int i = 0; i < dp.size(); i++) {
            for (int j = 0; j < dp[i].size(); j++) {
                if (used[i][j] && map[i][j] != 0 && dp[i][j] >= maxV) {
                    if (dp[i][j] == maxV && used[i][j]) {
                        number = max(number, map[i][j]);
                    } else {
                        number = map[i][j];
                    }
                    maxV = dp[i][j];
                    last.first = i;
                    last.second = j;
                }
                maxV = max(maxV, dp[i][j]);
            }
        }

        cout << maxV << " " << number << endl;

    }

    return 0;
}






















































Measured memory_bytes: 35016