#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int powmod(long long a, long long b) {
  long long res = 1;
  if (a >= mod) a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a;
    if (res >= mod) res %= mod;
    a = a * a;
    if (a >= mod) a %= mod;
  }
  return res;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long i, j, n, m, l[1000], c[1000];
  map<long long, long long> mm;
  cin >> n;
  for (i = 0; i < n; i++) cin >> l[i];
  for (i = 0; i < n; i++) {
    cin >> c[i];
    if (mm.find(l[i]) == mm.end()) {
      mm[l[i]] = c[i];
    } else {
      mm[l[i]] = min(mm[l[i]], c[i]);
    }
  }
  set<long long> ss;
  set<long long>::iterator it;
  for (i = 0; i < n; i++) {
    set<long long> tempset;
    for (it = ss.begin(); it != ss.end(); it++) {
      long long temp = gcd(l[i], *it);
      long long cost = c[i] + mm[*it];
      if (mm.find(temp) == mm.end())
        mm[temp] = cost;
      else
        mm[temp] = min(mm[temp], cost);
      tempset.insert(temp);
    }
    for (it = tempset.begin(); it != tempset.end(); it++) {
      ss.insert(*it);
    }
    ss.insert(l[i]);
  }
  if (mm.find(1) == mm.end())
    cout << "-1";
  else
    cout << mm[1];
}
