#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::cerr;

using std::abs;
using std::min;
using std::max;
using std::swap;

using std::map;
using std::pair;
using std::set;
using std::string;
using std::vector;

using std::sort;
using std::reverse;

using ll = long long;

#define forn(i, n) for (ll i = 0; i < (ll) n; ++i)
#define sz(a) static_cast<int>(a.size())
#define push_back emplace_back
#define endl '\n'

class solver {
private:
    double TIME;

    static const ll INF = static_cast<ll>(1e9) + 7;
    static const int MAXN = static_cast<ll>(1e2) + 17;
    const int MAGIC = 51;

    int n;
    int A[MAXN], B[MAXN];
    bool dp[MAXN][MAXN][MAXN];

public:
    static bool FLAG;

    solver() {
        TIME = static_cast<double>(clock()) / CLOCKS_PER_SEC;
    }

    bool read() {
        if (!(cin >> n))
            return false;

        forn (i, n)
            cin >> A[i];

        forn (i, n)
            cin >> B[i];

        return true;
    }

    void solve() {
        for (int a = 0; a < MAGIC; ++a)
            for (int mod = 1; mod <= MAGIC; ++mod)
                dp[a][a][mod] = 1;

        for (int a = 0; a < MAGIC; ++a)
            for (int b = 0; b < MAGIC; ++b)
                for (int mod = 1; mod <= MAGIC; ++mod)
                    for (int mod1 = 1; mod1 <= MAGIC; ++mod1)
                        dp[a][b][mod] |= dp[a % mod][b][mod1];

        bool ok = true;

        for (int i = 0; i < n; ++i) {
            bool neok = true;

            for (int mod = 1; mod <= MAGIC; ++mod)
                if (dp[A[i]][B[i]][mod])
                    neok = false;

            if (neok)
                ok = false;
        }

        if (!ok) {
            cout << -1 << endl;
            return ;
        }

        ll ans = 0;
        vector<ll> masks(n);

        for (int i = 0; i < n; ++i)
            masks[i] = (1ll << A[i]);

        for (int t = MAGIC; t > 0; --t) {
            bool good = true;

            for (int i = 0; i < n; ++i) {
                bool ns = true;

                for (int j = 0; j < MAGIC; ++j)
                    if ((masks[i] >> j) & 1) {
                        if (j == B[i])
                            ns = false;

                        for (int t1 = 1; t1 < t; ++t1)
                            for (int t2 = 1; t2 <= t1; ++t2)
                                if (dp[j % t1][B[i]][t2])
                                    ns = false;
                    }

                if (ns)
                    good = false;
            }

            if (!good) {
                ans += (1ll << t);

                for (int i = 0; i < n; ++i)
                    for (int j = 0; j < MAGIC; ++j)
                        if ((masks[i] >> j) & 1)
                            masks[i] |= (1ll << (j % t));
            }
        }

        cout << ans << endl;
    }

    ~solver() {
#ifdef SEREZHKA
        if (FLAG)
            cerr << "Time elapsed: " << static_cast<double>(clock()) / CLOCKS_PER_SEC - TIME << endl;
#endif
    }
};

bool solver::FLAG = true;

int main() {
    cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(10);
    cerr << std::fixed << std::setprecision(5);

#ifdef SEREZHKA
    freopen("file.in", "r", stdin);
    std::mt19937 rand('S' + 'E' + 'R' + 'E' + 'Z' + 'H' + 'K' + 'A');
#else
    std::mt19937 rand(time(NULL));
#endif

    while (solver::FLAG) {
        solver solution;

        if (!solution.read())
            solver::FLAG = false;
        else
            solution.solve();
    }

    return 0;
}


Measured memory_bytes: 768