#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


vector<int> divisor(int n) {
    vector<int> res;
    for (int i = 1; i*i <= n; ++i) {
        if (n%i == 0LL) {
            res.push_back(i);
            int temp = n/i;
            if (i != temp) res.push_back(temp);
        }
    }
    sort(res.begin(), res.end());
    return res;
}

vector<int> divs[55];

int N;
vector<int> a;
vector<int> b;

int INF = 1<<29;
bool seen[51][51];
int dp[51][51];
int rec(int a, int b) {
    if (seen[a][b]) return dp[a][b];
    if (a == b) return 0;
    if (b == 0) return 1;
    if (a < b || (a > b && a <= b * 2)) return -1;
    int res = INF;
    for (int d = 1; d < a; ++d) {
        int tmp = rec(a % d, b);
        if (tmp != -1 && tmp != INF) chmin(res, max(d, tmp));
    }
    seen[a][b] = true;
    if (res == INF) res = -1;
    
    //cout << a << ", " << b << ": " << res << endl;
    
    return dp[a][b] = res;
}

// d 未満の bit だけでできるか？
 bool can(int d, vector<int> alr) {
     int n = (int)alr.size();
     for (int i = 0; i < N; ++i) {
         bool ok = false;
         
         int max = rec(a[i], b[i]);
         if (max != -1 && max < d) ok = true;
         
         for (int k = 1; k <= 4; ++k) {
             if (ok) break;
             for (int bit = (1<<k)-1, BX, BY; bit < (1<<n); BX = bit&-bit, BY = bit+BX, bit = ((bit&~BY)/BX>>1)|BY) {
                 if (ok) break;
                 int na = a[i];
                 for (int j = 0; j < alr.size(); ++j) {
                     if (bit & (1<<j)) na %= alr[j];
                 }
                 int max = rec(na, b[i]);
                 if (max != -1 && max < d) ok = true;
             }
         }
         if (!ok) return false;
     }
     return true;
 }


int Max[51];

int main() {
    memset(seen, 0, sizeof(seen));
    memset(dp, -1, sizeof(dp));
    
    /*
    int d = 0;
    for (int a = 1; a <= 50; ++a) {
        for (int b = 1; b <= 50; ++b) {
            chmax(d, rec(a, b));
            if (rec(a, b) != -1) cout << a << ", " << b << ": " << rec(a, b) << endl;
        }
    }
    COUT(d);
     */

    
    while (cin >> N) {
        a.resize(N);
        b.resize(N);
        for (int i = 0; i < N; ++i) cin >> a[i];
        for (int i = 0; i < N; ++i) cin >> b[i];
        
        int maxd = 0;
        bool first = true;
        for (int i = 0; i < N; ++i) {
            if (rec(a[i], b[i]) == -1) first = false;
            chmax(maxd, rec(a[i], b[i]));
        }
        if (!first) {
            cout << -1 << endl;
            continue;
        }
        
        
        long long res = 0;
        vector<int> vec;
        for (int d = maxd; d > 0; --d) {
            if (!can(d, vec)) {
                res += (1LL<<d);
                if (vec.size() == 0) {
                    for (int i = 0; i < N; ++i) {
                        if (rec(a[i], b[i]) == d) {
                            a[i] %= d;
                        }
                    }
                }
                vec.push_back(d);
            }
        }
        cout << res << endl;
    }
}





























Measured memory_bytes: 256