#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
vector<int> g;
map<int, int> mp;
void add(int v) {
  if (!mp[v]) {
    mp[v] = g.size() + 1;
    g.push_back(v);
  }
}
void go(int v) {
  for (int i = 1; i * i <= v; ++i) {
    if (v % i == 0) {
      add(i);
      add(v / i);
    }
  }
}
int get(int v) {
  int val = mp[v] - 1;
  return val;
}
int a[1005];
int c[1005];
int c1[1000005];
int c2[1000005];
int gcd(int a, int b) {
  while (a > 0 && b > 0) {
    if (a > b)
      a %= b;
    else
      b %= a;
  }
  return (a + b);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    go(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  int M = g.size();
  for (int i = 0; i < n + 5; ++i)
    for (int j = 0; j < M + 5; ++j) c1[j] = c2[j] = INF;
  for (int i = 0; i < n; ++i) {
    int cur = get(a[i]);
    c2[cur] = min(c1[cur], c[i]);
    for (int j = 0; j < M; ++j) {
      int cur = gcd(g[j], a[i]);
      cur = get(cur);
      c2[cur] = min(c2[cur], c1[j] + c[i]);
    }
    for (int j = 0; j < M; ++j) {
      c1[j] = min(c1[j], c2[j]);
      c2[j] = INF;
    }
  }
  if (c1[0] == INF) c1[0] = -1;
  cout << c1[0] << endl;
  return 0;
}
