def extgcd(a,b) return [1,0] if b == 0 y,x = extgcd(b, a % b) y -= (a/b)*x return [x,y] end def mod_inverse(a, mod) x,y = extgcd(a, mod) return x % mod end def chinese_remainder(m1,m2,a,b) u1, u2 = extgcd(m1, m2) return nil if (a - b) % m1.gcd(m2) != 0 l = (a - b) / (m1.gcd(m2)) return (a - m1 * u1 * l) % (m1 * m2 / m1.gcd(m2)) end n = gets.to_i x = Array.new(n) y = Array.new(n) n.times do |i| x[i], y[i] = gets.split.map(&:to_i) end ans, mod = 0, 1 x.zip(y).each do |x,y| ans = chinese_remainder(mod, y, ans, x) unless ans puts -1 exit end mod = mod * y / mod.gcd(y) end ans += mod if ans == 0 puts ans % 1000000007