fun main(args: Array<String>?): Unit {
    val prime = primes(999983)
    while(true) {
        val n = readLine()!!.trim().toInt()
        if (n == 0) break
        var result = 0L
        repeat(n){
            val (p, m) = readLine()!!.trim().split(' ').map(String::toInt)
            result += prime.upperBound(p + m) - prime.lowerBound(p - m) - 1
        }
        println(maxOf(0, result))
    }
}
fun primes(upperLimit: Int): List<Int> {
    val isPrime = BooleanArray(upperLimit + 1){true}
    for (i in 2 .. upperLimit) {
        if (i * i > upperLimit) break
        if (isPrime[i]) for (j in i * i .. upperLimit step i){
            isPrime[j] = false
        }
    }
    return (2 .. upperLimit).filter(isPrime::get)
}
fun <T: Comparable<T>> List<T>.lowerBound(value: T): Int {
    var from = 0
    var until = size
    while (from < until){
        val mid = (from + until) / 2
        if (this[mid] < value) from = mid + 1
        else until = mid
    }
    return until
}
fun <T: Comparable<T>> List<T>.upperBound(value: T): Int {
    var from = 0
    var until = size
    while (from < until) {
        val mid = (from + until) / 2
        if (this[mid] <= value) from = mid + 1
        else until = mid
    }
    return until
}
