#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int bm = 200001;
int BIT[bm];
void add(int A) {
	while (A > 0) {
		BIT[A]++;
		A -= A & -A;
	}
}
int query(int A) {
	int ret = 0;
	while (A <= bm) {
		ret += BIT[A];
		A += A & -A;
	}
	return ret;
}
ll A[200001], B[200001], tmp[200001];
void pakuri_sort(int N, ll A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if (*ci++ == '0') {
				tmp = tmp * 10;
				ci++;
			}
		}
	}
	return tmp;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N = getint();
	rep1(i, N) A[i] = ll(i) << 32 | i + getint();
	rep1(i, N) B[i] = ll(i) << 32 | i + getint();

	pakuri_sort(N, A + 1);
	pakuri_sort(N, B + 1);

	rep1(i, N) {
		if ((A[i] ^ B[i]) & (1ll << 32) - 1) {
			co(-1);
			return 0;
		}
		tmp[A[i] >> 32] = B[i] >> 32;
	}
	ll kotae = 0;
	rep1(i, N) {
		kotae += query(tmp[i]);
		add(tmp[i]);
	}
	co(kotae);

	Would you please return 0;
}
#include <bits/stdc++.h>
using namespace std;

// #include <atcoder/all>
// using namespace atcoder;

#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;


void solve(long long N, std::vector<long long> X){
    set<int> all;
    vector<vector<int>> v(N, vector<int>(50));
    rep(i, N) {
        ll x = X[i];
        set<int> primes;
        for (ll j = 2; j <= x; j++) {
            while (x % j == 0) {
                primes.insert(j);
                x /= j;
            }
        }
        for (int p: primes) {
            all.insert(p);
            v[i][p] = 1;
        }
    }
    vector<int> al;
    for (auto a: all) {
        al.push_back(a);
    }
    int s = al.size();
    ll ans = 1001001001001001001;
    for (int b = 0; b < (1 << s); b++) {
        vector<int> seen(N, 0);
        ll tmp = 1;
        rep(i, N) {
            for (int bi = 0; bi < s; bi++) {
                if ((b >> bi) & 1) {
                    if (v[i][al[bi]] == 1) {
                        seen[i] = 1;
                        break;
                    }
                }
            }
        }
        int cnt = 0;
        for (auto e: seen) {
            cnt += e;
        }
        if (cnt == N) {
            for (int bi = 0; bi < s; bi++) {
                if ((b >> bi) & 1) {
                    tmp *= al[bi];
                }
            }
            ans = min(ans, tmp);
        }
    }
    cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> X(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&X[i]);
    }
    solve(N, std::move(X));
    return 0;
}
