#include <bits/stdc++.h>
using namespace std;
const int N = 300;
const int M = 1 << 10;
const long long INF = 1e18;
const long double EPS = 1e-9;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long random(long long a, long long b) {
  return uniform_int_distribution<long long>(a, b)(rng);
}
int n;
vector<long long> a;
vector<long long> cost;
vector<long long> GetPrimes(long long num) {
  vector<long long> result;
  if (num % 2 == 0) {
    result.push_back(2);
    while (num % 2 == 0) num /= 2;
  }
  for (long long i = 3; i * i <= num; i += 2) {
    if (num % i == 0) {
      result.push_back(i);
      while (num % i == 0) num /= i;
    }
  }
  if (num > 1) result.push_back(num);
  return result;
}
long long current_pos, full_mask;
vector<set<long long>> primes;
long long dp[N][M];
bool done[N][M];
void TurnOn(int& mask, int bit) { mask = mask | (1 << bit); }
long long DP(int pos, int mask) {
  if (mask == full_mask) return 0;
  if (pos == n) return INF;
  if (done[pos][mask]) return dp[pos][mask];
  dp[pos][mask] = DP(pos + 1, mask);
  if (pos != current_pos) {
    int new_mask = mask;
    int bit = 0;
    for (int prime : primes[current_pos]) {
      if (primes[pos].count(prime) == 0) TurnOn(new_mask, bit);
      bit++;
    }
    dp[pos][mask] = min(dp[pos][mask], cost[pos] + DP(pos + 1, new_mask));
  }
  done[pos][mask] = true;
  return dp[pos][mask];
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  a = vector<long long>(n);
  cost = vector<long long>(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> cost[i];
  for (int i = 0; i < n; i++) {
    set<long long> new_primes;
    if (a[i] != 1) {
      vector<long long> current_primes = GetPrimes(a[i]);
      for (long long prime : current_primes) new_primes.insert(prime);
    }
    primes.push_back(new_primes);
  }
  long long result = INF;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      result = min(result, cost[i]);
    } else {
      current_pos = i;
      full_mask = (1 << ((int)primes[i].size())) - 1;
      for (int i = 0; i < n; i++) {
        for (int mask = 0; mask < full_mask; mask++) {
          done[i][mask] = false;
        }
      }
      result = min(result, cost[i] + DP(0, 0));
    }
  }
  if (result < INF) {
    cout << result << '\n';
  } else {
    cout << -1 << '\n';
  }
  return 0;
}
