#include <bits/stdc++.h>
using namespace std;
const int N = 300 + 10, MASK = (1 << 10) + 10, inf = 1e9;
int l[N], c[N], dp[N][MASK];
vector<int> primes(int n) {
  vector<int> res;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      while (n % i == 0) n /= i;
      res.push_back(i);
    }
  }
  if (n > 1) res.push_back(n);
  return res;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  int ans = inf;
  for (int i = 0; i < n; i++) {
    swap(l[i], l[0]);
    swap(c[i], c[0]);
    vector<int> v = primes(l[0]);
    for (int mask = 0; mask < (1 << v.size()); mask++) dp[0][mask] = inf;
    dp[0][(1 << v.size()) - 1] = c[0];
    for (int j = 1; j < n; j++) {
      int x = 0;
      for (int k = 0; k < v.size(); k++)
        if (l[j] % v[k] == 0) x += (1 << k);
      for (int mask = 0; mask < (1 << v.size()); mask++)
        dp[j][mask] = dp[j - 1][mask];
      for (int mask = 0; mask < (1 << v.size()); mask++) {
        if (dp[j - 1][mask] < inf)
          dp[j][mask & x] = min(dp[j][mask & x], dp[j - 1][mask] + c[j]);
      }
    }
    ans = min(ans, dp[n - 1][0]);
  }
  cout << (ans < inf ? ans : -1) << "\n";
  return 0;
}
