require 'prime'

Max_prime = 1000000
@prime = [false] * (Max_prime + 1)
Prime.each(Max_prime).to_a.each do |i|
    @prime[i] = true
end

def solve(table, sx, sy, max_n)
    max = 0
    cur_n = 0
    dp = 1001.times.map{[0] * 1001}
    y = sy
    l = r = sx
    if @prime[table[sy][sx]]
        dp[sy][sx] = 1
        max, cur_n = 1, table[sy][sx]
    end
    loop do
        (l..r).each do |x|
            a = table[y + 1][x + 1] if inCheck(x + 1, y + 1, table, max_n)
            b = table[y + 1][x] if inCheck(x, y + 1, table, max_n)
            c = table[y + 1][x - 1] if inCheck(x - 1, y + 1, table, max_n)
            pre = dp[y][x]
            max, cur_n = update(pre, dp, a, x + 1, y + 1, max, cur_n) if a
            max, cur_n = update(pre, dp, b, x, y + 1, max, cur_n) if b
            max, cur_n = update(pre, dp, c, x - 1, y + 1, max, cur_n) if c
        end
        l -= 1 if inCheck(l - 1, y + 1, table, max_n)
        r += 1 if inCheck(r + 1, y + 1, table, max_n)
        y += 1
        break if y >= 1000 || (l..r).to_a.all?{ |x| !inCheck(x, y, table, max_n) }
    end
    [max, cur_n]
end

def update(pre, dp, n, x, y, max, cur_n)
    if dp[y][x] < pre + 1 && @prime[n]
        dp[y][x] = pre + 1
        max, cur_n = dp[y][x], n if max < dp[y][x] || max == dp[y][x] && cur_n < n
    elsif dp[y][x] < pre && !@prime[n]
        dp[y][x] = pre
    end
    [max, cur_n]
end

def inCheck(x, y, table, n)
    0 <= x && x <= 1000 && 0 <= y && y <= 1000 &&
    table[y][x] <= n
end

def tabulate
    table = 1001.times.map{[0] * 1001}
    place = (Max_prime + 1).times.map{ [] }
    dir = [[1, 0], [0, -1], [-1, 0], [0, 1]]
    c = 0
    n = 1
    x = 500
    y = 500

    while n <= Max_prime do
        dir.each_with_index do |(dx, dy), i|
            c += 1 if i == 0
            c += 1 if i == 2
            c.times do
                table[y][x] = n
                place[n] = [x, y]
                x += dx; y += dy
                n += 1
            end
        end
    end
    [table, place]
end

table, place = tabulate
loop do
    m, n = gets.split.map(&:to_i)
    break if m == 0 && n == 0
    sx, sy = place[n]
    puts solve(table, sx, sy, m).join(' ')
end