#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
#include<cmath>
#include<set>
#include<random>
#include<map>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
const long long mod = 1000000007;
const long long inf = 11451419198109130;
typedef pair<int, int> P;
typedef pair<int,P> PP;
struct edge { int to, cor; };

int a[55], b[55];
vector<int>G[55];
int graf[55][55];
int used[55];//0...つかっていい

signed main() {
	int n; cin >> n;
	rep(i, n)cin >> a[i];
	rep(i, n)cin >> b[i];
	rep(i, n)G[i].push_back(a[i]);
	int ans = 0;

	for (int m = 50; m >= 0; m--) {//mまで使う
		rep(i, 51)rep(j, 51)graf[i][j] = inf;
		rep(i, 51) {
			for (int k = 0; k <= 50; k++) {
				if (k == 0)graf[i][i] = 0;
				else if (!used[k])graf[i][i%k] = min(graf[i][i%k], 1ll << k);
			}
		}

		rep(i, 51)rep(j, 51)rep(k, 51)graf[j][k] = min(graf[j][k], graf[j][i] + graf[i][k]);

		int flg = 0;
		rep(i, n) { if (graf[a[i]][b[i]] == inf)flg++; }
		if (flg) {
			if (m == 50) {
				cout << -1 << endl;
				return 0;
			}
			else used[m + 1] = 0;
		}
		used[m]++;
	}

	rep(i, 51)if (!used[i])ans += 1ll << i;
	cout << ans << endl;
}