def extgcd(a, b): if b == 0: return (a, 1, 0) (g, y, x) = extgcd(b, a % b) return (g, x, y - (a // b) * x) N = int(raw_input()) - 1 ans, add = map(int,raw_input().strip().split()); if ans == 0: ans = ans + add while N > 0: X, Y = map(int,raw_input().strip().split()) g, a, b = extgcd(add, Y) now = ans % Y need = X - now if need < 0: need = need + Y if need % g != 0: ans = -1; break need = need / g ans = ans + (add * a) * need add = add * Y / g ans = ans % add if ans < 0: ans = ans + add print(ans)