#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
vector<int> primes;
int dp[300][1 << 9];
int l[300];
int c[300];
int selected;
int n;
int rec(int i, int mask) {
  if (i == n) {
    if (mask) return (400 * 100000);
    return 0;
  }
  if (dp[i][mask] != -1) return dp[i][mask];
  if (selected == i) return dp[i][mask] = rec(i + 1, mask) + c[i];
  int nm = mask;
  for (int j = 0; j < primes.size(); j++) {
    if (l[i] % primes[j]) nm = nm & (~(1 << j));
  }
  return dp[i][mask] = min(rec(i + 1, nm) + c[i], rec(i + 1, mask));
}
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  int res = (400 * 100000);
  for (selected = 0; selected < n; ++selected) {
    memset(dp, -1, sizeof(dp));
    long long x = l[selected];
    primes.clear();
    for (long long p = 2; p * p <= x; p += 1LL + (p & 1LL)) {
      if (x % p) continue;
      primes.push_back(p);
      while (x % p == 0) x /= p;
    }
    if (x > 1) primes.push_back(x);
    res = min(res, rec(0, (1 << primes.size()) - 1));
  }
  if (res == (400 * 100000))
    cout << -1 << endl;
  else
    cout << res << endl;
}
