#include <bits/stdc++.h>
using namespace std;
int n, l[1001], c[1001];
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
map<int, int> M;
int main() {
  scanf("%d", &(n));
  for (int(i) = (0); (i) < (n); (i)++) scanf("%d", &(l[i]));
  for (int(i) = (0); (i) < (n); (i)++) scanf("%d", &(c[i]));
  for (int(i) = (0); (i) < (n); (i)++) {
    vector<pair<int, int> > aux;
    aux.push_back(make_pair(l[i], c[i]));
    for (map<int, int>::iterator it = M.begin(); it != M.end(); it++) {
      aux.push_back(make_pair(gcd(it->first, l[i]), it->second + c[i]));
    }
    for (int(j) = (0); (j) < (aux.size()); (j)++) {
      int caux = M[aux[j].first];
      if (caux == 0)
        M[aux[j].first] = aux[j].second;
      else
        M[aux[j].first] = min(caux, aux[j].second);
    }
  }
  if (M[1] == 0) M[1] = -1;
  cout << M[1] << endl;
}
