import java.util.*

fun main(args:Array<String>?): Unit {
    val isPrime = BooleanArray(1000002){true}
    for (i in 2 .. 1001) if (isPrime[i]){
        for (j in i * i .. 1000001 step i){
            isPrime[j] = false
        }
    }
    isPrime[0] = false
    isPrime[1] = false
    val length = IntArray(1000002){-1}
    while(true){
        val (m, n) = readLine()!!.trim().split(' ').map(String::toInt)
        if (m == 0 && n == 0) return
        val start = fromNumber(n)
        val queue: Queue<Triple<Int, Int, Int>> = LinkedList()
        queue.add(Triple(start.first, start.second, n))
        length[n] = if (isPrime[n]) 1 else 0
        var maxPrime = if (isPrime[n]) n else 0
        var maxLength = length[n]
        while(queue.isNotEmpty()){
            val (x, y, current) = queue.poll()
            for (nx in x - 1 .. x + 1) {
                val suspect = fromPosition(nx, y + 1)
                if (suspect <= m) {
                    if (length[suspect] == -1){
                        queue.add(Triple(nx, y + 1, suspect))
                    }
                    if (isPrime[suspect]) {
                        length[suspect] = Math.max(length[current] + 1, length[suspect])
                        if (maxLength == length[suspect] && maxPrime < suspect) maxPrime = suspect
                        else if (maxLength < length[suspect]) {
                            maxPrime = suspect
                            maxLength = length[suspect]
                        }
                    }else {
                        length[suspect] = Math.max(length[current], length[suspect])
                    }
                }
            }
        }
        (0 .. m).forEach { length[it] = -1 }
        println(if(maxLength == 0) "0 0" else "$maxLength $maxPrime")
    }

}
fun fromPosition(x: Int, y: Int): Int {
    val max = Math.max(Math.abs(x), Math.abs(y))
    return if (y > 0 && y == max) (max * 2 + 1) * (max * 2 + 1) - max + x
    else if (y < 0 && y == - max) (max * 2 - 1) * (max * 2 - 1) + max * 2 + max - x
    else if (x > 0) (max * 2 - 1) * (max * 2 - 1) + max - y
    else (max * 2 - 1) * (max * 2 - 1) + 5 * max + y
}
fun fromNumber(number: Int): Pair<Int, Int> {
    val size = (0 until number).find { (it * 2 + 1) * (it * 2 + 1) >= number }!!
    for (x in -size .. size) {
        if (fromPosition(x, size) == number) return Pair(x, size)
        if (fromPosition(x, -size) == number) return Pair(x, -size)
    }
    for (y in -size .. size){
        if (fromPosition(size, y) == number) return Pair(size, y)
        if (fromPosition(-size, y) == number) return Pair(-size, y)
    }
    TODO()
}

