#include <bits/stdc++.h>

using namespace std;

int n, x[55], y[55];
bool u[55], g, o[55];
long long ans;

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		scanf("%d", x + i);
	for (int i = 0; i < n; i++)
		scanf("%d", y + i);
	for (int i = 1; i <= 51; i++)
		u[i] = 1;
	for (int i = 51; i; i--) {
		u[i] = 0;
		g = 0;
		for (int j = 0; j < n; j++) {
			for (int k = 0; k <= 50; k++)
				o[k] = 0;
			o[x[j]] = 1;
			for (int k = 50; k; k--)
				if (u[k])
					for (int l = 0; l <= 50; l++)
						if (o[l])
							o[l % k] = 1;
			if (!o[y[j]]) {
				g = 1;
				break;
			}
		}
		if (g)
			if (i == 51) {
				printf("-1\n");
				return 0;
			} else {
				u[i] = 1;
				ans ^= 1LL << i;
			}
	}
	printf("%lld\n", ans);
	return 0;
}