require 'prime'
require 'set'

$primes = Set[*Prime.each(1000000).to_a]

loop {
	m, n = gets.split.map &:to_i
	break if m == 0
	
	y = max_x = max_y = min_x = min_y = 0
	x = -1
	dx = 1
	dy = 0
	k = 2
	
	hash = {}
	m.times {|i|
		x += dx
		y += dy
		if x < min_x
			min_x = x
			dx, dy = -dy, dx
		elsif x > max_x
			max_x = x
			dx, dy = -dy, dx
		elsif y < min_y
			min_y = y
			dx, dy = -dy, dx
		elsif y > max_y
			max_y = y
			dx, dy = -dy, dx
		end
	
		hash[[x, y]] = i+1
	}
	
	a = (min_y..max_y).map{ [0] * (max_x-min_y+1) }
	hash.each{|(x, y), k|
		a[y-min_y][x-min_x] = k
	}
	
	table = (min_x..max_x).map{[0,-1]}
	
	catch(:exit) {
		loop {
			t = []
			b = a.shift
			(0..b.size-1).each{|i|
				c, j = table[[i-1, 0].max..[i+1, b.size-1].min].max
				t[i] = [$primes.include?(b[i]) ? c+1 : c, c == 0 ? b[i] : j]
				if b[i] == n
					puts t[i][0] == 0 ? '0 0' : t[i].join(" ")
					throw :exit
				end
			}
			table = t
		}
	}
}