// 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <random>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

const int B = 60;
int N, a[B], b[B];

int func(vector<pii> &ope) {
    int ans = 0;
    rep(z,0,N) {
        int i = ope[z].second;
        int s = a[i], t = b[i];
        if(s < t) {
            return INF;
        }

        vector<int> dist(B, INF), mon(B);
        dist[s] = 0;
        rep(k,0,B) {
            if(!(ans >> k & 1)) mon[k] = (1LL << k);
        }

        priority_queue< pii, vector<pii>, greater<pii> > que;
        que.push(make_pair(dist[s], s));

        while(que.size()) {
            pii cur = que.top(); que.pop();
            int cost = cur.first, mo = cur.second;
            for(int k=1; k<=mo; k++) {
                int n_mo = mo % k;
                int n_cost = cost + mon[k];
                if(dist[n_mo] > n_cost) {
                    dist[n_mo] = n_cost;
                    que.push(make_pair(dist[n_mo], n_mo));
                }
            }
        }
        ans |= dist[t];
    }
    return ans;
}

signed main() {
    cin >> N;
    
    bool ok = true;
    rep(i,0,N) cin >> a[i];
    rep(i,0,N) cin >> b[i];

    vector<pii> ope;
    rep(i,0,N) {
        int s = a[i], t = b[i];
        // printf("s = %lld, t = %lld\n", s, t);
        if(s < t) {
            ok = false;
            continue;
        }

        vector<int> dist(B, INF);
        dist[s] = 0;

        priority_queue< pii, vector<pii>, greater<pii> > que;
        que.push(make_pair(dist[s], s));

        while(que.size()) {
            pii cur = que.top(); que.pop();
            int cost = cur.first, mo = cur.second;
            for(int k=1; k<=mo; k++) {
                int n_mo = mo % k;
                int n_cost = cost + (1LL << k);
                if(dist[n_mo] > n_cost) {
                    dist[n_mo] = n_cost;
                    que.push(make_pair(dist[n_mo], n_mo));
                }
            }
        }
        ope.push_back(make_pair(dist[t], i));
    }

    int ans = INF;
    mt19937 engine(114514);
    rep(i,0,4000) {
        shuffle(ope.begin(), ope.end(), engine);
        chmin(ans, func(ope));
    }

    if(ans == INF) ok = false;
    cout << (ok ? ans : -1) << endl;
    return 0;
}

Measured memory_bytes: 384