package yukicoder; import java.util.Scanner; public class Main { private static final int MOD = 1000000007; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] X = new int[N]; int[] Y = new int[N]; for (int i = 0; i < N; i++) { X[i] = sc.nextInt(); Y[i] = sc.nextInt(); } for (int i = 1; i < N; i++) { int[] cmt = cmt(X[0], Y[0], X[i], Y[i]); if (cmt[0] < 0) { System.out.println(-1); return; } X[0] = cmt[0]; Y[0] = cmt[1]; } if (X[0] == 0) { X[0] = Y[0]; } System.out.println(X[0]); } static int[] extgcd(int a, int b, int[] is) { if (a == 0) { is[0] = 0; is[1] = 1; is[2] = b; } else { extgcd(b % a, a, is); int x = is[1] - b / a * is[0]; is[1] = is[0]; is[0] = x; } return is; } static int[] cmt(int a1, int mo1, int a2, int mo2) { int[] exgcd = extgcd(mo1, mo2, new int[3]); int g = exgcd[2]; int x = exgcd[0]; a1 %= mo1; a2 %= mo2; if (a1 % exgcd[2] != a2 % exgcd[2]) { return new int[] { -1, 0 }; } int lcm = mo1 * (mo2 / g); int v = a1 + ((a2 - a1) % lcm) * x % lcm * (mo1 / g); return new int[] { ((v % lcm) + lcm) % lcm, lcm }; } }