import numpy as np n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) ab = [] for a,b in zip(A,B): ab.append(abs(a-b)) ab = list(set(ab)) x = np.gcd.reduce(ab) if x == 1 and ab!=[1]: print(-1) else: print(np.lcm.reduce(ab))