#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i, s, t) for(i = s; i <= t; ++i)
#define dep(i, s, t) for(i = s; i >= t; --i)

using namespace std;

const int N = 55;
int n;
int a[N][N], b[N];
int tmp[N];
long long ans;

void cp(int *a, int *b) {
	int i;
	rep(i, 0, 50) b[i] = a[i];
}

bool check(int *a, int S, int p) {
	int i, j;
	dep(i, S, 1)
		rep(j, 1, 50) a[j%i] |= a[j];
	return a[p];
}

int main() {
	int i, j, k, x; bool f;
	scanf("%d", &n);
	rep(i, 1, n) {scanf("%d", &x); a[i][x] = 1;}
	rep(i, 1, n) scanf("%d", &b[i]);
	
	rep(i, 1, n) {
//		rep(j, 1, n) tmp[j] = a[i][j];
		cp(a[i], tmp);
		if(!check(tmp, 50, b[i])) {puts("-1"); return 0;}
	}
	dep(i, 50, 1) {
		f = 0;
		rep(j, 1, n) {
			cp(a[j], tmp);
			if(!check(tmp, i-1, b[j])) {f = 1; break;}
		}
		if(f) {
			rep(j, 1, n)
				rep(k, 1, 50)
					a[j][k%i] |= a[j][k];
			ans += 1ll << i;
		}
	}
	printf("%lld\n", ans);
	return 0;
}