Problem:
Score : 700 points

Problem Statement

Aoki is playing with a sequence of numbers a_{1}, a_{2}, ..., a_{N}. Every second, he performs the following operation :

Choose a positive integer k. For each element of the sequence v, Aoki may choose to replace v with its remainder when divided by k, or do nothing with v. The cost of this operation is 2^{k} (regardless of how many elements he changes).

Aoki wants to turn the sequence into b_{1}, b_{2}, ..., b_{N} (the order of the elements is important). Determine if it is possible for Aoki to perform this task and if yes, find the minimum cost required.

Constraints

1 \leq N \leq 50

0 \leq a_{i}, b_{i} \leq 50

All values in the input are integers.

Input

Input is given from Standard Input in the following format:

N
a_{1} a_{2} ... a_{N}
b_{1} b_{2} ... b_{N}

Output

Print the minimum cost required to turn the original sequence into b_{1}, b_{2}, ..., b_{N}. If the task is impossible, output -1 instead.

Sample Input 1

3
19 10 14
0 3 4

Sample Output 1

160

Here's a possible sequence of operations :

Choose k = 7. Replace 19 with 5, 10 with 3 and do nothing to 14. The sequence is now 5, 3, 14.

Choose k = 5. Replace 5 with 0, do nothing to 3 and replace 14 with 4. The sequence is now 0, 3, 4.

The total cost is 2^{7} + 2^{5} = 160.

Sample Input 2

3
19 15 14
0 0 0

Sample Output 2

2

Aoki can just choose k = 1 and turn everything into 0. The cost is 2^{1} = 2.

Sample Input 3

2
8 13
5 13

Sample Output 3

-1

The task is impossible because we can never turn 8 into 5 using the given operation.

Sample Input 4

4
2 0 1 8
2 0 1 8

Sample Output 4

0

Aoki doesn't need to do anything here. The cost is 0.

Sample Input 5

1
50
13

Sample Output 5

137438953472

Beware of overflow issues.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

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

#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef complex<double> point;
#define xx real()
#define yy imag()

#define REP(i, a, b) for(int i = (a); i < (int)(b); i++)
#define REPN(i, a, b) for(int i = (a); i <= (int)(b); i++)
#define FA(it, x) for(__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define SZ(x) (int)(x).size()
#define BE(x) (x).begin(), (x).end()
#define SORT(x) sort(BE(x))
#define _1 first
#define _2 second

#define x1 gray_cat_x1
#define y1 gray_cat_y1

template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define file "I1"

const double EPS = 1e-9;
const double PI = acos(-1.);
const int INF = 1e9;
const ll MOD = 1e9 + 7;

const int MAXN = 51;

int dp[MAXN][MAXN];

int a[MAXN], b[MAXN];

ll ans;

void rec_sol(vector<ll> &v) {
	int mx = 0;
	REP(i, 0, SZ(v)) {
		int mn = INF;
		REP(j, 0, MAXN) {
			if (v[i] & (1ll << j)) {
				mn = min(mn, dp[j][b[i]]);
			}
		}
		mx = max(mx, mn);
	}
	
	if (mx == 0) {
		return;
	}
	
	/*REP(i, 0, SZ(v)) {
		REP(j, 0, MAXN) {
			if (v[i] & (1ll << j)) {
				printf("%d ", j);
			}
		}
		putchar('\n');
	}
	printf("%d\n", mx);
	printf("**********************\n");*/
	
	ans |= (1ll << mx);
	
	REP(i, 0, SZ(v)) {
		ll mask = 0ll;
		REP(j, 0, MAXN) {
			if ((v[i] & (1ll << j)) && dp[j][b[i]] <= mx) {
				int tmp = j % mx;
				if (dp[tmp][b[i]] < mx) {
					mask |= (1ll << tmp);
				}
				if (dp[j][b[i]] < mx) {
					mask |= (1ll << j);
				}
			}
		}
		v[i] = mask;
	}
	
	rec_sol(v);
}

void solve(){
	REP(i, 0, MAXN) {
		REP(j, 0, MAXN) {
			dp[i][j] = INF;
		}
		dp[i][i] = 0;
	}
	REP(d, 1, MAXN) {
		REP(i, 0, MAXN) {
			REP(j, 0, MAXN) {
				if (i % d == j && dp[i][j] == INF) {
					dp[i][j] = d;
					REP(k, 0, MAXN) {
						if (dp[j][k] < INF) {
							dp[i][k] = min(dp[i][k], d);
						}
					}
				}
			}
		}
	}
	int n;
	scanf("%d", &n);
	REP(i, 0, n) {
		scanf("%d", &a[i]);
	}
	REP(i, 0, n) {
		scanf("%d", &b[i]);
	}
	
	int mx = 0;
	REP(i, 0, n) {
		mx = max(mx, dp[a[i]][b[i]]);
	}
	if (mx == INF) {
		printf("-1\n");
		return;
	}
	
	vector<ll> vv(n);
	REP(i, 0, n) {
		vv[i] = (1ll << a[i]);
	}
	rec_sol(vv);
	
	printf("%lld\n", ans);
}

int main(){

    //freopen(file".in", "r", stdin); freopen(file".out", "w", stdout);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();    
    }
}