### constants

MAX_P = 1000000

### subroutines

def gen_primes(max_p)
  primes = Array.new(max_p + 1){true}
  primes[0] = primes[1] = false
  num_p = 0

  for p in (2..max_p)
    if primes[p]
      num_p += 1
      for q in (p * p).step(max_p, p)
        primes[q] = false
      end
    end
  end

  return primes
end

### main

primes = gen_primes(MAX_P)

while true
  n = gets.strip.to_i
  break if n == 0

  kx = 0

  n.times.each do
    p, m = gets.strip.split(' ').map{|s| s.to_i}
    #puts "#{p}, #{m}"

    i0 = [p - m, 2].max
    i1 = [p + m, MAX_P].min

    x = 0

    for i in (i0..i1)
      if primes[i]
        x += 1
      end
    end

    kx += (x - 1)
  end

  puts kx
end