#include <iostream>
#include <vector>
#include <set>

using namespace std;

int main()
{
    int N;
    cin>>N;
    vector<int> a(N), b(N);
    for (int &t: a)
        cin>>t;
    for (int &t: b)
        cin>>t;
    for (int i=0; i<N; i++)
        if (b[i]!=a[i] && (a[i]==0 || b[i]>(a[i]-1)/2))
        {
            cout<<-1<<endl;
            return 0;
        }
    set<int> K;
    for (int k=50; k>0; k--)
    {
        bool f = false;
        for (int i=0; i<N; i++)
        {
            long long A = 1LL<<a[i];
            for (int t=50; t>0; t--)
                if (t<k || K.count(t)>0)
                    for (int j=0; j<=51; j++)
                        if (A&1LL<<j)
                            A |= 1LL<<(j%t);
            if (!(A&1LL<<b[i]))
                f = true;
        }
        if (f)
            //cout<<k<<endl,
            K.insert(k);
    }
    long long ans = 0;
    for (int k: K)
        ans += 1LL<<k;
    cout<<ans<<endl;
}
