#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using ld = long double;
mt19937 rnd(time(nullptr));
const lli inf = 1e18;
lli Bit(lli mask, lli i) { return (mask >> i) & 1; }
lli SetBitZero(lli mask, lli i) {
  if (Bit(mask, i) == 1) mask ^= (1LL << i);
  return mask;
}
inline void Solve() {
  lli n = 0;
  cin >> n;
  vector<pair<lli, lli>> data(n);
  for (auto &it : data) cin >> it.first;
  for (auto &it : data) cin >> it.second;
  lli result = inf;
  for (auto &it : data) {
    vector<lli> primes;
    lli value = it.first;
    for (lli j = 2; j * j <= value; j++) {
      if (value % j == 0) {
        primes.push_back(j);
        while (value % j == 0) value /= j;
      }
    }
    if (value > 1) primes.push_back(value);
    lli sz = primes.size();
    vector<lli> dp((1LL << sz), inf);
    dp[(1LL << sz) - 1] = it.second;
    for (lli mask = (1LL << sz) - 1; mask >= 0; mask--) {
      for (lli i = 0; i < n; i++) {
        lli mask1 = mask;
        for (lli j = 0; j < primes.size(); j++) {
          if (data[i].first % primes[j] != 0) mask1 = SetBitZero(mask1, j);
        }
        dp[mask1] = min(dp[mask1], dp[mask] + data[i].second);
      }
    }
    result = min(result, dp[0]);
  }
  if (result == inf)
    cout << -1;
  else
    cout << result;
}
signed main() {
  cout << fixed << setprecision(20);
  Solve();
  return 0;
}
