#include <bits/stdc++.h>
using namespace std;
char BUFFER[100000 + 5];
bool readn(int &n) { return scanf("%d", &n) == 1; }
bool readl(long long &n) { return scanf("%I64d", &n) == 1; }
bool readd(double &n) { return scanf("%lf", &n) == 1; }
bool reads(string &s) {
  s = "";
  int n = scanf("%s", BUFFER);
  if (n == 1) s = BUFFER;
  return n == 1;
}
bool readln(string &s) {
  char *valid = gets(BUFFER);
  if (valid) s = BUFFER;
  return ((bool)valid);
}
vector<int> L, C, v, dp;
void doit(int n) {
  int i;
  for (i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      v.push_back(i);
      v.push_back(n / i);
    }
  }
}
template <class T>
T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int i, j, k, d, n;
  cin >> n;
  L.resize(n);
  C.resize(n);
  v.clear();
  for (i = 0; i < ((int)L.size()); ++i) {
    cin >> L[i];
    doit(L[i]);
  }
  for (i = 0; i < ((int)C.size()); ++i) cin >> C[i];
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  dp.resize(v.size());
  for (i = 0; i < ((int)dp.size()); ++i) dp[i] = 1e9;
  for (i = 0; i < ((int)L.size()); ++i) {
    k = lower_bound(v.begin(), v.end(), L[i]) - v.begin();
    dp[k] = min(dp[k], C[i]);
  }
  for (i = 0; i < ((int)L.size()); ++i)
    for (j = 0; j < ((int)v.size()); ++j)
      if (dp[j] != 1e9) {
        d = gcd(L[i], v[j]);
        k = lower_bound(v.begin(), v.end(), d) - v.begin();
        dp[k] = min(dp[k], C[i] + dp[j]);
      }
  if (dp[0] == 1e9) dp[0] = -1;
  cout << dp[0] << endl;
  return 0;
}
