#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<long long int> a, w;
vector<long long int> getR(long long int x) {
  vector<long long int> ans;
  for (long long int i = 2; i * i <= x; ++i) {
    if (x % i == 0) {
      ans.push_back(i);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) ans.push_back(x);
  return ans;
}
long long int getMask(long long int x, vector<long long int>& r) {
  long long int ans = 0;
  for (long long int i = 0; i < r.size(); ++i) {
    if (x % r[i] == 0) ans ^= (1 << i);
  }
  return ans;
}
long long int getAns(long long int ind) {
  vector<long long int> r = getR(a[ind]);
  long long int p = (long long int)r.size();
  swap(a[0], a[ind]);
  swap(w[0], w[ind]);
  vector<vector<long long int>> dp(n, vector<long long int>(1ll << p, 1e15));
  dp[0][getMask(a[0], r)] = w[0];
  for (long long int i = 1; i < n; ++i) {
    for (long long int old = 0; old < (1ll << p); ++old) {
      long long int now = getMask(a[i], r);
      dp[i][old] = min(dp[i][old], dp[i - 1][old]);
      dp[i][old & now] = min(dp[i][old & now], dp[i - 1][old] + w[i]);
    }
  }
  swap(a[0], a[ind]);
  swap(w[0], w[ind]);
  return dp[n - 1][0];
}
signed main() {
  cin >> n;
  a.resize(n);
  w.resize(n);
  for (long long int i = 0; i < n; ++i) cin >> a[i];
  for (long long int i = 0; i < n; ++i) cin >> w[i];
  long long int ans = 1e15;
  for (long long int i = 0; i < n; ++i) ans = min(ans, getAns(i));
  if (ans > 1e13)
    cout << -1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
