#!/usr/bin/ruby def egcd(x,y) return[x,1,0]if y==0 g,a,b=egcd(y,x%y) [g,b,a-x/y*b] end def chinese(a) return nil if !a||a.empty? a.reduce([0,1]){|(a1,m1),(a2,m2)| g,x,y=egcd(m1,m2) v=(a2-a1)/g break if v*g!=a2-a1 lcm=m1/g*m2 [(a1+v*x*m1)%lcm,lcm] } end r=chinese($<.drop(1).map{|e|e.split.map &:to_i}) p r ?(r[0]==0?r[1]:r[0])%1000000007:-1