#include <iostream>
#include <vector>

using namespace std;

bool check(int a, int b, const vector<int>& div, int low){
	if(a < b) return false;
	vector<int> dp(a+1, 0);
	dp[a] = 1;
	for(auto& d : div){
		for(int i=d;i<=a;i++){
			if(dp[i]) dp[i%d] = 1;
		}
	}
	for(int d=low;d>0;d--){
		for(int i=d;i<=a;i++){
			if(dp[i]) dp[i%d] = 1;
		}
	}
	return dp[b];
}

bool checkAll(const vector<int>& a, const vector<int>& b, const vector<int>& div, int low){
	const int N = a.size();
	for(int i=0;i<N;i++){
		if(!check(a[i], b[i], div, low)) return false;
	}
	return true;
}

int main(){
	int N;
	while(cin >> N){
		vector<int> a(N), b(N);
		for(auto& t : a) cin >> t;
		for(auto& t : b) cin >> t;
		vector<int> res;
		int end = 50;
		while(true){
			if(checkAll(a, b, res, 0)) break;
			bool update = false;
			for(int i=1;i<=end;i++){
				if(checkAll(a, b, res, i)){
					res.push_back(i);
					end = i - 1;
					update = true;
				}
			}
			if(!update) break;
		}
		if(!checkAll(a, b, res, 0)){
			cout << -1 << endl;
		} else {
			long long c = 0;
			for(auto& t : res) c += 1LL << t;
			cout << c << endl;
		}
	}
}
