from math import gcd def extgcd(a, b, x1=1, y1=0, x2=0, y2=1): if b == 0: return a, x1, y1 q = a//b return extgcd(b, a%b, x2 ,y2, x1-x2*q, y1-y2*q) def crt2(a, b, m1, m2): if a < b: a, b = b, a m1, m2 = m2, m1 d, im1, im2 = extgcd(m1, m2) if a%d != b%d: return None mm = m1//d*m2 x = (a - ((a-b)//d)*(m1*im1)) % mm return x, mm def crt(as_, ms): x = as_[0] mm = ms[0] for a, m in zip(as_[1:], ms[1:]): t = crt2(a, x, m, mm) if t is None: return None x, mm = t return x, mm n = int(input()) as_, ms = zip(*(map(int, input().split()) for _ in range(n))) t = crt(as_, ms) if t is None: print(-1) else: x, mm = t if x == 0: x = mm print(x)