#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define mod 1000000007LL
#define eps 1e-13
// #define PI 3.141592653589793238L
#define INF 1000000011
#define INFLL 1000000000000000011LL
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define mp make_pair
#define F first
#define S second
#define pb push_back
#define fo(i,a,n) for(i = (a); i < (n); i++)
#define gtl(x) getline(cin, (x))
#define flsh fflush(stdout)
#define sws ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define gcd __gcd
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)((a).size())
#define io_file freopen("D:/Coding Problems/Contest/input_file.in", "r", stdin); freopen("D:/Coding Problems/Contest/output_file.out", "w", stdout)

ll modx(ll Base, ll exponent)
{
	ll ans = 1;
	if(Base == 1)
		return Base;
	while(exponent)
	{
		if(exponent & 1)
			ans = (ans * Base)%mod;
		Base = (Base * Base)%mod;
		exponent = exponent >> 1;
	}
	return ans;
}

ll inmodx(ll num)
{
	return (modx(num, mod-2LL));
}

bool cmp(pair < pii, int > a, pair < pii, int > b)//true for a before b
{
	if(a.F.F/300 < b.F.F/300)
		return 1;
	if(a.F.F/300 > b.F.F/300)
		return 0;
	if(a.F.S < b.F.S)
		return 1;
	if(a.F.S > b.F.S)
		return 0;
	if(a.F <= b.F)
		return 1;
	return 0;
}

const int N = (50) + 9;
const int M = (N<<2) + 9;
const int LOGN = ((int)log2(N)) + 3;
const int LOGM = ((int)log2(M)) + 3;
const int BUCK = 2*550;
const int SQRT = BUCK+9;

const int MAX = 50;
int a[N], b[N];
ll p2[N];
int v[N][N];
bool dist[N][N];
set < int > st[N], temp;

void dfs(int cur, int root)
{
	dist[root][cur] = 1;
	for(int i = 1; i <= cur; i++)
		if(!dist[root][v[cur][i]])
			dfs(v[cur][i],root);
	return;
}

void pre()
{
	int i, j;
	fo(i,1,MAX+1)
	{
		fo(j,1,i+1)
			v[i][j] = i%j;
		v[i][0] = i;
	}
	fo(i,0,MAX+1)
		dfs(i,i);
	return;
}

void solve()
{
	int n, j, i, k;
	bool flag, inner_flag;
	ll ans = 0;
	p2[0] = 1;
	fo(i,1,N)
		p2[i] = p2[i-1]+p2[i-1];
	pre();
	cin >> n;
	fo(i,1,n+1)
		cin >> a[i];
	fo(i,1,n+1)
		cin >> b[i];
	fo(i,1,n+1)
		if(!dist[a[i]][b[i]])
		{
			cout << -1 << '\n';
			return;
		}
	fo(i,1,n+1)
		st[i].insert(a[i]);
	for(j = MAX; j > 0; j--)
	{
		flag = 1;
		fo(i,1,n+1)
		{
			inner_flag = 0;
			for(auto x : st[i])
			{
				if(x == b[i])
					inner_flag = 1;
				if(inner_flag)
					break;
				fo(k,1,j)
					if(dist[v[x][k]][b[i]])
					{
						// if(j <= 8)
						// 	cout << i << " : " << x << " " << k << " " << v[x][k] << '\n';
						inner_flag = 1;
						break;
					}
			}
			if(!inner_flag)
			{
				flag = 0;
				break;
			}
		}
		if(!flag)// take and dont take
		{
			fo(i,1,n+1)
			{
				temp.clear();
				for(auto x : st[i])
				{
					temp.insert(x);
					temp.insert(x%j);
				}
				st[i] = temp;
			}
			ans += p2[j];
		}
		// if(j <= 8)
		// {
		// 	cout << "AT " << j << " :\n";
		// 	fo(i,1,n+1)
		// 	{
		// 		cout << i << " : ";
		// 		for(auto x : st[i])
		// 			cout << x << " ";
		// 		cout << '\n';
		// 	}
		// 	cout << '\n';
		// }
	}
	cout << ans << '\n';
	return;
}

int main()
{
	sws;
	clock_t clk;
	clk = clock();
	// io_file;
	// srand (time(NULL));

	//Code here
	int t = 1, cs;
	cout << fixed << setprecision(9);
	// cin >> t;
	fo(cs,1,t+1)
	{
		// cout << "Case #" << cs << ": ";
		solve();
	}
	// Code ends here

	clk = clock() - clk;
	cerr << fixed << setprecision(6) << "Time: " << ((double)clk)/CLOCKS_PER_SEC << "\n";
	return 0;
}

Measured memory_bytes: 384