#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
#include <functional>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <list>
#include <sstream>
#include <cstring>
#include <stdio.h>
#include <complex>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

typedef long double LD;
typedef unsigned long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<LD, LD> PDD;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<char> VCH;
typedef vector<LD> VLD;
typedef vector<string> VS;
typedef vector<VS> VSS;
typedef vector<VI> VVI;
typedef vector<VLL> VVLL;
typedef vector<VCH> VVCH;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<PDD> VPDD;
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define next fake_next
#define prev fake_prev
#define left fake_left
#define right fake_right

#define FOR(i, a, b) for(LL i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, t) FOR(i, 0, t)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))

const LD PI = acos(-1.0);
const LD EPS = 1e-6;
const LL INF = 1e9;
const LL LINF = 8e18;
const LL mod = 1e9 + 7;
const LL MAX = 50 + 7;

void bad()
{
	cout << -1;
	exit(0);
}

int memo[MAX][MAX][MAX + 1];
vector<int> mozhna;
int KKK[51][51][52];

int can(int a, int b, int max_k);

int dwa(int  a, int b, int max_k)
{
	for (auto i : mozhna)
		if (a >= i && can(a%i, b, min(i - 1, max_k)))
			return KKK[a][b][max_k] = i;

	return 0;
}

int can(int a, int b, int max_k)
{
	if (a < b)
		return memo[a][b][max_k] = 0;

	if (a == b)
		return memo[a][b][max_k] = 1;

	if (memo[a][b][max_k] != -1)
		return memo[a][b][max_k];

	RFOR(i, max_k + 1, 1)
		if (a >= i && can(a%i, b, i - 1))
			return memo[a][b][max_k] = 1;

	if (dwa(a, b, max_k))
		return memo[a][b][max_k] = 1;

	return memo[a][b][max_k] = 0;
}

LL p2[MAX];

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("In.txt", "r", stdin);
	p2[0] = 1;
	FOR(i, 1, MAX)
		p2[i] = 2LL * p2[i - 1];

	int n;
	cin >> n;
	VI a(n), b(n);
	FOR(i, 0, n)
		cin >> a[i];

	FOR(i, 0, n)
		cin >> b[i];

	FOR(i, 0, n)
		if (a[i] < b[i])
			bad();

	LL ans = 0;

	while (1)
	{
		FILL(memo, -1);
		FILL(KKK, -1);
		bool vse_ok = 1;
		VI min_k(n, -1);

		FOR(i, 0, n)
		{
			if (can(a[i], b[i], 0))
				continue;

			if (!can(a[i], b[i], MAX))
				bad();

			vse_ok = 0;
			FOR(k, 0, MAX + 1)
				if (can(a[i], b[i], k))
				{
					min_k[i] = k;
					break;
				}
		}

		if (vse_ok)
			break;

		int ma = 0;
		FOR(i, 0, n)
			ma = max(ma, min_k[i]);

		if (ma == 0)
			throw - 1;

		ans += p2[ma];
		FOR(i, 0, n)
			if (min_k[i] == ma)
			{
				if (KKK[a[i]][b[i]][min_k[i]] != -1)
					a[i] %= KKK[a[i]][b[i]][min_k[i]];

				a[i] %= ma;
			}

		mozhna.push_back(ma);
		sort(ALL(mozhna));
		reverse(ALL(mozhna));
	}

	cout << ans;
	cin >> n;
	return 0;
}

Measured memory_bytes: 1536