#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 100;
#define LL long long
int F[N][N][N], G[N][N], mx, ai[N], bi[N];
LL ans;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++ i) cin >> ai[i], mx = max(mx, ai[i]);
    for (int i = 1; i <= n; ++ i) cin >> bi[i];
    for (int i = 0; i <= mx; ++ i) F[0][i][i] = 1;
    for (int i = 1; i <= mx; ++ i)
        for (int j = 0; j <= mx; ++ j)
            for (int k = 0; k <= j; ++ k)
                F[i][j][k] = F[i - 1][j][k] || F[i - 1][j % i][k];
    for (int i = 1; i <= n; ++ i) if (!F[mx][ai[i]][bi[i]]) {cout << -1 << endl; return 0;}
    for (int i = 1; i <= n; ++ i) G[i][ai[i]] = 1;
    for (int i = mx; i >= 1; -- i)
    {
        int p = 1;
        for (int j = 1; j <= n; ++ j)
        {
            int q = 0;
            for (int k = 0; k <= ai[j]; ++ k)
                if (G[j][k] && F[i - 1][k][bi[j]])
                    q = 1;
            p &= q;
        }
        if (!p)
        {
            ans |= 1ll << i;
            for (int j = 1; j <= n; ++ j)
                for (int k = 0; k <= ai[j]; ++ k)
                    if (G[j][k]) G[j][k % i] = 1;
        }
    }
    cout << ans << endl;
}
