#include<bits/stdc++.h>

using namespace std;

int N, a[109], b[109];
bool dp[109][109];

bool canDo (long long msk)
{
    for (int i=0; i<=50; i++)
        for (int j=0; j<=50; j++)
            dp[i][j] = (i == j);
    for (int i=0; i<=50; i++)
        for (int j=1; j<=50; j++)
            if (msk & (1LL << j))
                dp[i][i % j] = 1;
    for (int k=0; k<=50; k++)
        for (int i=0; i<=50; i++)
            for (int j=0; j<=50; j++)
                dp[i][j] |= (dp[i][k] & dp[k][j]);
    bool ok = 1;
    for (int i=1; i<=N; i++)
        ok &= dp[a[i]][b[i]];
    return ok;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d", &N);
for (int i=1; i<=N; i++)
    scanf ("%d", &a[i]);
for (int i=1; i<=N; i++)
    scanf ("%d", &b[i]);
long long msk = (1LL << 51) - 2;
if (!canDo (msk))
{
    printf ("-1\n");
    return 0;
}
for (int i=50; i>0; i--)
    if (canDo (msk ^ (1LL << i)))
        msk ^= 1LL << i;
printf ("%lld\n", msk);
return 0;
}
