#include<bits/stdc++.h>
using namespace std;

const int N = 55;
typedef long long ll;

int n, a[N], b[N];
bool var[N], vis[N];
ll cost;
queue <int> q;

bool Bfs(int s, int t) {
	memset(vis, 0, sizeof(vis));
	vis[s] = 1;
	while(!q.empty()) q.pop();
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		for(int i = u; i > 0; i--) 
			if(!vis[u%i] && var[i]) {
				vis[u%i] = 1;
				q.push(u%i);
			}
	}
	return vis[t];
}

bool check() {
	for(int i = 1; i <= n; i++) 
		if(!Bfs(a[i], b[i])) return false;
	return true;
}

int main() {
	cin>>n;
	for(int i = 1; i <= n; i++) 
		cin>>a[i];
	for(int i = 1; i <= n; i++) 
		cin>>b[i];
	memset(var, 1, sizeof(var));
	for(int i = 50; i >= 1; i--) {
		if(!check()) {
			cout<<-1<<endl;
			return 0;
		}
	    var[i] = 0;
	    if(!check()) var[i] = 1;
	}
	for(int i = 1; i <= 50; i++)
		if(var[i]) cost |= 1ll<<i;
	cout<<cost<<endl;
	return 0;
}