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 garner(b, m, md): m.append(md) x = [0]*len(m) mm = [1]*len(m) for i in range(len(b)): _, inv, _ = extgcd(mm[i], m[i]) for j in range(i+1, len(m)): x[j] += mm[j]*((b[i] - x[i]) * inv % m[i]) x[j] %= m[j] mm[j] *= m[i] mm[j] %= m[j] return x[-1], mm[-2] def pre_garner(b, m): for i in range(len(m)): for j in range(len(m)): if i == j: continue g = gcd(m[i], m[j]) if (b[i]-b[j])%g != 0: return False m[i] //= g m[j] //= g gi = gcd(g, m[i]) gj = g//gi while g != 1: g = gcd(gi, gj) gi *= g gj //= g m[i] *= gi m[j] *= gj b[i] %= m[i] b[j] %= m[j] return True n = int(input()) b, m = zip(*(tuple(map(int, input().split())) for _ in range(n))) b, m = list(b), list(m) if not pre_garner(b, m): print(-1) else: x, mm = garner(b, m, 10**9+7) if all(bi==0 for bi in b): print(mm) else: print(x%(10**9+7))