def extgcd(a, b): if b: d, y, x = extgcd(b, a%b) y -= (a/b)*x return d, x, y else: return a, 1, 0 n = input() d = 1 x = 0 v = [] for i in xrange(n): X, Y = map(int, raw_input().split()) g, a, b = extgcd(d, Y) x, d = (Y*b*x + d*a*X)/g, d*(Y / g) x = (x + d) % d v.append((X, Y)) for X, Y in v: if x % Y != X: print -1 exit(0) print x