require 'prime'
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 i.prime?
    end
    if cnt > 1 then x += cnt - 1
    else x -= 1 - cnt
    end
  end
  p x
end