#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxE(T& a, const T& b) {
  a = max(a, b);
}
template <typename T>
void minE(T& a, const T& b) {
  a = min(a, b);
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& t_) {
  out << "[";
  for (auto i : t_) out << i << ", ";
  out << "]";
  return out;
}
template <typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
  out << "(" << rhs.first << "," << rhs.second << ")";
  return out;
}
int gcd(int a, int b) {
  while (a && b) a > b ? a %= b : b %= a;
  return a + b;
}
map<int, int> t;
void proc(int l, int c) {
  if (0)
    cout << "proc "
         << "l"
         << " " << l << " "
         << "c"
         << " " << c << " " << endl;
  if (t.find(l) == t.end()) {
    t[l] = c;
    if (0) cout << "init0" << endl;
  } else if (t[l] > c) {
    if (0) cout << "relax1" << endl;
    t[l] = c;
  }
  vector<pair<int, int> > tmp;
  for (auto i : t) tmp.push_back(i);
  for (auto i : tmp) {
    int g = gcd(i.first, l);
    if (t.find(g) == t.end() || t[g] > i.second + c) {
      if (0)
        cout << "relax2 "
             << "i"
             << " " << i << " "
             << "g"
             << " " << g << " "
             << "l"
             << " " << l << " "
             << "c"
             << " " << c << " "
             << "i.nd + c"
             << " " << i.second + c << " " << endl;
      t[g] = i.second + c;
    }
  }
}
int main() {
  int n;
  assert(scanf("%d", &n) == 1);
  int l[n];
  for (int i = 0; i < int(n); ++i) assert(scanf("%d", l + i) == 1);
  int c[n];
  for (int i = 0; i < int(n); ++i) assert(scanf("%d", c + i) == 1);
  for (int i = 0; i < int(n); ++i) proc(l[i], c[i]);
  if (t.find(1) == t.end())
    printf("-1\n");
  else
    printf("%d\n", t[1]);
  return 0;
}
