#include <bits/stdc++.h>
using namespace std;
void Duxar(int _this_line) {}
template <class T>
void ReadNo(T &_value) {
  T sign = 1;
  char ch;
  _value = 0;
  while (!isdigit(ch = getchar())) {
    (ch == '-') && (sign = -1);
  }
  do {
    _value = _value * 10 + (ch - '0');
  } while (isdigit(ch = getchar()));
  _value *= sign;
}
template <class T>
void AddNr(T &a, T b) {
  a = a + b;
  while (a >= 1000000007) {
    a -= 1000000007;
  }
  while (a < 0) {
    a += 1000000007;
  }
}
int N;
long long ans = (long long)1 << 60;
vector<vector<int> > primes;
vector<long long> jump, cost, dp(1024);
int main() {
  int i, j, k, q, p, mask, to;
  long long update;
  ReadNo(N);
  primes.resize(N);
  jump.resize(N);
  cost.resize(N);
  for (i = 0; i < N; ++i) {
    ReadNo(jump[i]);
    long long aux = jump[i];
    for (j = 2; j * j <= aux; j++) {
      if (aux % j == 0) {
        primes[i].push_back(j);
        while (aux % j == 0) {
          aux /= j;
        }
      }
    }
    if (aux > 1) {
      primes[i].push_back(aux);
    }
  }
  for (i = 0; i < N; ++i) {
    ReadNo(cost[i]);
  }
  for (i = 0; i < N; ++i) {
    fill((dp).begin(), (dp).end(), (long long)1 << 60);
    int sm = primes[i].size();
    int states = 1 << sm;
    dp[states - 1] = cost[i];
    for (j = 0; j < N; ++j) {
      if (i == j) continue;
      k = primes[j].size();
      mask = 0;
      for (q = 0; q < sm; ++q) {
        for (p = 0; p < k; ++p) {
          if (primes[i][q] == primes[j][p]) {
            mask |= (1 << q);
            break;
          }
        }
      }
      for (k = 0; k < states; ++k) {
        if (dp[k] != (long long)1 << 60) {
          to = k & mask;
          update = dp[k] + cost[j];
          dp[to] = min(dp[to], update);
        }
      }
    }
    ans = min(ans, dp[0]);
  }
  if (ans == (long long)1 << 60) ans = -1;
  cout << ans;
  putchar('\n');
  return 0;
}
