from scipy.sparse.csgraph import dijkstra





def make_graph(m, subset):

    matrix = [[0] * (m + 1) for _ in range(m + 1)]

    for k in subset:

        for s in range(k, m + 1):

            matrix[s][s % k] = 1

    return dijkstra(matrix)





def solve(n, aaa, bbb):

    for a, b in zip(aaa, bbb):

        if a < b:

            return -1

    m = max(aaa)

    subset = set(range(1, m + 1))

    sp = make_graph(m, subset)

    if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)):

        return -1



    for k in range(m, 0, -1):

        subset.remove(k)

        sp = make_graph(m, subset)

        if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)):

            subset.add(k)

    return sum(1 << k for k in subset)





n = int(eval(input()))

aaa = list(map(int, input().split()))

bbb = list(map(int, input().split()))

print((solve(n, aaa, bbb)))

from scipy.sparse.csgraph import floyd_warshall

import numpy as np





def make_graph(m):

    matrix = np.zeros([m + 1, m + 1, m + 1])

    for k in range(1, m + 1):

        for s in range(k, m + 1):

            matrix[k, s, s % k] = 1

    return matrix





def is_disable(matrix, subset, aaa, bbb):

    if subset:

        sub_matrix = matrix[list(subset)].max(axis=0)

    else:

        sub_matrix = matrix[0]

    sp = floyd_warshall(sub_matrix)

    return any(sp[a, b] > 50 for a, b in zip(aaa, bbb))





def solve(aaa, bbb):

    for a, b in zip(aaa, bbb):

        if a < b:

            return -1

    m = max(aaa)

    matrix = make_graph(m)

    subset = set(range(1, m + 1))

    if is_disable(matrix, subset, aaa, bbb):

        return -1



    for k in range(m, 0, -1):

        subset.remove(k)

        if is_disable(matrix, subset, aaa, bbb):

            subset.add(k)

    return sum(1 << k for k in subset)





n = int(eval(input()))

aaa = list(map(int, input().split()))

bbb = list(map(int, input().split()))

print((solve(aaa, bbb)))

from scipy.sparse.csgraph import dijkstra


def make_graph(m, subset):
    matrix = [[0] * (m + 1) for _ in range(m + 1)]
    for k in subset:
        for s in range(k, m + 1):
            matrix[s][s % k] = 1
    return dijkstra(matrix)


def solve(n, aaa, bbb):
    for a, b in zip(aaa, bbb):
        if a < b:
            return -1
    m = max(aaa)
    subset = set(range(1, m + 1))
    sp = make_graph(m, subset)
    if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)):
        return -1
    for k in range(m, 0, -1):
        subset.remove(k)
        sp = make_graph(m, subset)
        if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)):
            subset.add(k)
    return sum(1 << k for k in subset)


n = int(eval(input()))
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print((solve(n, aaa, bbb)))

from scipy.sparse.csgraph import floyd_warshall
import numpy as np


def make_graph(m):
    matrix = np.zeros([m + 1, m + 1, m + 1])
    for k in range(1, m + 1):
        for s in range(k, m + 1):
            matrix[k, s, s % k] = 1
    return matrix


def is_disable(matrix, subset, aaa, bbb):
    if subset:
        sub_matrix = matrix[list(subset)].max(axis=0)
    else:
        sub_matrix = matrix[0]
    sp = floyd_warshall(sub_matrix)
    return any(sp[a, b] > 50 for a, b in zip(aaa, bbb))


def solve(aaa, bbb):
    for a, b in zip(aaa, bbb):
        if a < b:
            return -1
    m = max(aaa)
    matrix = make_graph(m)
    subset = set(range(1, m + 1))
    if is_disable(matrix, subset, aaa, bbb):
        return -1
    for k in range(m, 0, -1):
        subset.remove(k)
        if is_disable(matrix, subset, aaa, bbb):
            subset.add(k)
    return sum(1 << k for k in subset)


n = int(eval(input()))
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
print((solve(aaa, bbb)))

-from scipy.sparse.csgraph import dijkstra +from scipy.sparse.csgraph import floyd_warshall +import numpy as np -def make_graph(m, subset): -    matrix = [[0] * (m + 1) for _ in range(m + 1)] -    for k in subset: +def make_graph(m): +    matrix = np.zeros([m + 1, m + 1, m + 1]) +    for k in range(1, m + 1): -            matrix[s][s % k] = 1 -    return dijkstra(matrix) +            matrix[k, s, s % k] = 1 +    return matrix -def solve(n, aaa, bbb): +def is_disable(matrix, subset, aaa, bbb): +    if subset: +        sub_matrix = matrix[list(subset)].max(axis=0) +    else: +        sub_matrix = matrix[0] +    sp = floyd_warshall(sub_matrix) +    return any(sp[a, b] > 50 for a, b in zip(aaa, bbb)) + + +def solve(aaa, bbb): +    matrix = make_graph(m) -    sp = make_graph(m, subset) -    if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)): +    if is_disable(matrix, subset, aaa, bbb): -        sp = make_graph(m, subset) -        if any(sp[a, b] > 50 for a, b in zip(aaa, bbb)): +        if is_disable(matrix, subset, aaa, bbb): -print((solve(n, aaa, bbb))) +print((solve(aaa, bbb)))