prime = Array.new(1_000_000, true)
prime[0] = prime[1] = false
(2..1000).each do |i|
  if prime[i]
    (2..1_000_000/i).each do |j|
      prime[i*j] = false
    end
  end
end
loop do
  n = gets.to_i
  break if n == 0
  x = 0
  n.times do 
    cnt = 0
    p, m = gets.split.map(&:to_i)
    a = p > m ? p - m : 0
    b = p + m < 1_000_000 ? p + m : 1_000_000 - 1
    (a..b).each do |i|
      cnt += 1 if prime[i]
    end
    if cnt > 1 then x += cnt - 1
    else x -= 1 - cnt
    end
  end
  p x
end