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)) x = np.lcm.reduce(ab) print(-1 if x not in ab else x)