Aoki is playing with a sequence of numbers a_{1}, a_{2}, ..., a_{N}. Every second, he performs the following operation: Choose a positive integer k. For each element of the sequence v, Aoki may choose to replace v with its remainder when divided by k, or do nothing with v. The cost of this operation is 2^{k} (regardless of how many elements he changes). Aoki wants to turn the sequence into b_{1}, b_{2}, ..., b_{N} (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.

Constraints:
1 ≤ N ≤ 50
0 ≤ a_{i}, b_{i} ≤ 50
All values in the input are integers.

Input:
Input is given from Standard Input in the following format:
N
a_{1} a_{2} ... a_{N}
b_{1} b_{2} ... b_{N}

Output:
Print the minimum cost required to turn the original sequence into b_{1}, b_{2}, ..., b_{N}. If the task is impossible, output -1 instead.

Sample Input 1:
3
19 10 14
0 3 4

Sample Output 1:
160
Here's a possible sequence of operations:
Choose k = 7. Replace 19 with 5, 10 with 3 and do nothing to 14. The sequence is now 5, 3, 14.
Choose k = 5. Replace 5 with 0, do nothing to 3 and replace 14 with 4. The sequence is now 0, 3, 4.
The total cost is 2^{7} + 2^{5} = 160.

Sample Input 2:
3
19 15 14
0 0 0

Sample Output 2:
2
Aoki can just choose k = 1 and turn everything into 0. The cost is 2^{1} = 2.

Sample Input 3:
2
8 13
5 13

Sample Output 3:
-1
The task is impossible because we can never turn 8 into 5 using the given operation.

Sample Input 4:
4
2 0 1 8
2 0 1 8

Sample Output 4:
0
Aoki doesn't need to do anything here. The cost is 0.

Sample Input 5:
1
50
13

Sample Output 5:
137438953472
Beware of overflow issues.

# Buggy version:
def solve(bit_state, num, mod):
    for div in range(1, num + 1):
        if num % div == mod:
            cand[i].append(bit_state | (1 << div))
        if num % div > mod:
            solve(bit_state | (1 << div), num % div, mod)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cand = [[] for i in range(n)]
for i in range(n):
    solve(0, a[i], b[i])

ans = 0
for i in range(50):
    max_ = 0
    for j in range(n):
        min_ = 10 ** 5
        for num in cand[j]:
            min_ = min(min_, num.bit_length() - 1)
        max_ = max(max_, min_)

    if max_ == 0:
        print(ans)
        break
    if max_ == 10 ** 5:
        print(-1)
        break
    ans += 2 ** max_
    
    tmp = [[] for i in range(n)]
    for j in range(n):
        for num in cand[j]:
            if num & (1 << max_):
                tmp[j].append(num - (1 << max_))
            else:
                tmp[j].append(num)
    cand = tmp[0:]
            

# Fixed version:
def solve(bit_state, num, mod):
    for div in range(1, num + 1):
        if num % div == mod:
            cand[i].append(bit_state | (1 << div))
        if num % div > mod:
            solve(bit_state | (1 << div), num % div, mod)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cand = [[] for i in range(n)]
for i in range(n):
    solve(0, a[i], b[i])
    if a[i] == b[i]:
        cand[i].append(0)

ans = 0
while True:
    max_ = 0
    for j in range(n):
        min_ = 10 ** 5
        for num in cand[j]:
            min_ = min(min_, num.bit_length() - 1)
        max_ = max(max_, min_)

    if max_ == 0:
        print(ans)
        break
    if max_ == 10 ** 5:
        print(-1)
        break
    ans += 2 ** max_
    
    tmp = [[] for i in range(n)]
    for j in range(n):
        for num in cand[j]:
            if num & (1 << max_):
                tmp[j].append(num - (1 << max_))
            else:
                tmp[j].append(num)
    cand = tmp[0:]

# Buggy version:
import sys

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

same = 0
for i in range(n):
	if b[i]*2 >= a[i]:
		print(-1)
		sys.exit()
	elif a[i] == b[i]:
		same += 1
if same == n:
	print(0)
	sys.exit()

edges = [[] for _ in range(51)]
for i in range(1, 51):
	for j in range(1, i+1):
		edges[i].append([i%j, j])

available = [True for _ in range(51)]
visited = [False for _ in range(51)]

def dfs(start, goal, cur):
	global visited
	if cur == goal:
		return True
	for e, k in edges[cur]:
		if visited[e] == False and available[k] == True:
			visited[e] = True
			if dfs(start, goal, e) == True:
				return True
	return False

ans = 0
for k in range(50, 0, -1):
	available[k] = False
	ok = True
	for i in range(n):
		visited = [False for _ in range(51)]
		visited[a[i]] = True
		if dfs(a[i], b[i], a[i]) == False:
			ok = False
			break
	if not ok:
		ans += 2**k
		available[k] = True

print(ans)

# Fixed version:
import sys

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

same = 0
for i in range(n):
	if a[i] == b[i]:
		same += 1
	elif b[i]*2 >= a[i]:
		print(-1)
		sys.exit()

if same == n:
	print(0)
	sys.exit()

edges = [[] for _ in range(51)]
for i in range(1, 51):
	for j in range(1, i+1):
		edges[i].append([i%j, j])

available = [True for _ in range(51)]
visited = [False for _ in range(51)]

def dfs(start, goal, cur):
	global visited
	if cur == goal:
		return True
	for e, k in edges[cur]:
		if visited[e] == False and available[k] == True:
			visited[e] = True
			if dfs(start, goal, e) == True:
				return True
	return False

ans = 0
for k in range(50, 0, -1):
	available[k] = False
	ok = True
	for i in range(n):
		visited = [False for _ in range(51)]
		visited[a[i]] = True
		if dfs(a[i], b[i], a[i]) == False:
			ok = False
			break
	if not ok:
		ans += 2**k
		available[k] = True

print(ans)

# Buggy version:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
x,ans = [],0
def f(m):
	q = [i+1 for i in range(m)]+x
	c = [[True]*51 for i in range(51)]
	for i in q:
		for j in range(i,51): c[j][j%i] = False
	for k in range(51):
		for i in range(51):
			for j in range(51): c[i][j]*=bool(c[i][k]+c[k][j])
	return any(c[a[i]][b[i]] for i in range(n))
if f(50): print(-1)
else:
	for i in range(49,-1,-1):
		if f(i): x.append(i+1)
	for i in x: ans+=2**i
	print(ans)

# Fixed version:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
x,ans = [],0
def f(m):
	q = [i+1 for i in range(m)]+x
	c = [[True]*51 for i in range(51)]
	for i in range(51): c[i][i] = False
	for i in q:
		for j in range(i,51): c[j][j%i] = False
	for k in range(51):
		for i in range(51):
			for j in range(51): c[i][j]*=bool(c[i][k]+c[k][j])
	return any(c[a[i]][b[i]] for i in range(n))
if f(50): print(-1)
else:
	for i in range(49,-1,-1):
		if f(i): x.append(i+1)
	for i in x: ans+=2**i
	print(ans)