#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>

class Direct_Graph {
    public:
    struct Edge { 
        int to; long long cost;
    };
    std::vector<std::vector<Edge>> G;
    int node_num;
    Direct_Graph() {}
    Direct_Graph(const int node_num_in) {
        node_num = node_num_in;
        G.resize(node_num);
    }
    void add_edge(const int from, const int to, const long long cost) {
        Edge edge; edge.to = to; edge.cost = cost;
        G[from].push_back(edge);
    }
    void add_edge_both(const int node1, const int node2, const long long cost) {
        add_edge(node1, node2, cost);
        add_edge(node2, node1, cost);
    }

    std::vector<long long> Dijkstra(int s, const long long dist_inf) {
        std::vector<long long> dist; dist.resize(node_num);
        for (int i = 0; i < dist.size(); i++) {
            dist[i] = dist_inf;
        }
        typedef std::pair<long long, int> Pair;

        std::priority_queue <Pair, std::vector<Pair>, std::greater<Pair>> que;
        dist[s] = 0;
        que.push(Pair(0, s));

        while (!que.empty()) {
            Pair pair = que.top(); que.pop();
            int v = pair.second;
            if (dist[v] < pair.first) { continue; }
            for (Edge edge : G[v]) {
                if (dist[v] + edge.cost < dist[edge.to]) {
                    dist[edge.to] = dist[v] + edge.cost;
                    que.push(Pair(dist[edge.to], edge.to));
                }
            }
        }
        return dist;
    }

    std::vector<std::vector<long long>> Warshall_Floyd(const long long dist_inf) {
        std::vector<std::vector<long long>> dist; dist.resize(node_num);
        for (int i = 0; i < dist.size(); i++) {
            dist[i].resize(node_num);
            for (int j = 0; j < dist[i].size(); j++) {
                dist[i][j] = dist_inf;
            }
            dist[i][i] = 0;
            for (auto e : G[i]) {
                dist[i][e.to] = std::min(dist[i][e.to], e.cost);
            }
        }
        for (int k = 0; k < node_num; k++) {
            for (int i = 0; i < node_num; i++) {
                for (int j = 0; j < node_num; j++) {
                    dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        return dist;
    }
};

const int MAX_N = 55;
const long long MAX_DIST = ((long long)1) << MAX_N;
int N;
int a[MAX_N], b[MAX_N];

Direct_Graph dg = Direct_Graph(MAX_N);

int main(int argc, char **argv) {
    std::cin >> N;

    for (int i = 1; i <= N; i++) {
        std::cin >> a[i];
    }

    for (int i = 1; i <= N; i++) {
        std::cin >> b[i];
    }

    std::vector<int> S;

    while (true) {
        for (int i = 1; i <= 50; i++) {
            dg.G[i].clear();
        }

        int last = S.size() == 0 ? 50 : S[S.size() - 1];
        for (int i = 1; i <= 50; i++) {
            for (int j : S) {
                dg.add_edge(i, i % j, 0);
                //if (i <= 20) {
                //    std::cout << "edge:" << i << " " << i % j << std::endl;
                //}
            }
            for (int j = last - 1; 1 <= j; j--) {
                dg.add_edge(i, i % j, ((long long)1) << j);
            }
        }

        std::vector<std::vector<long long>> dist = dg.Warshall_Floyd(MAX_DIST);

        long long max = 0;
        for (int i = 1; i <= N; i++) {
            //if (S.size() == 2) {
            //    std::cout << i << " " << dist[a[i]][b[i]] << std::endl;
            //}
            max = std::max(max, dist[a[i]][b[i]]);
        }
        //std::cout << "max:" << max << std::endl;
        if (max == MAX_DIST) {
            assert(S.size() == 0);
            std::cout << -1 << std::endl;
            return 0;
        } else if (max == 0) {
            break;
        } else {
            int n = 0;
            while ((((long long)1) << n) <= max) {
                n++;
            }
            S.push_back(n - 1);
        }
    }

    long long ret = 0;
    for (int d : S) {
        ret += (((long long)1) << d);
    }
    std::cout << ret << std::endl;

    return 0;
}

Measured memory_bytes: 384