def exgcd(a,b,x,y):
if not b:
x,y = 1,0
return a,x,y
d,x,y = exgcd(b, a % b, y, x)
temp = y
y = x - a // b * y
x = temp
return d,x,y
if __name__ == "__main__":
n = int(input())
a1,m1 = map(int,input().split())
for i in range(n-1):
a2,m2 = map(int,input().split())
k1,k2 = 0, 0
d,k1,k2 = exgcd(a1,a2,k1,k2)
if (m2-m1) % d:
print(-1)
exit()
k1 *= (m2-m1) // d
t = a2 // d
k1 = k1 % t
m1 = a1 * k1 + m1
a1 = abs(a1 // d * a2)
print(m1 % a1)