#include <bits/stdc++.h>
using namespace std;
const int sigma_size = 26;
const int N = 100 + 50;
const int MAXN = 300 + 50;
const int inf = 0x3fffffff;
const double eps = 1e-8;
const int mod = 100000000 + 7;
int n;
int a[MAXN], c[MAXN];
map<int, int> mp;
vector<int> vec;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  while (~scanf("%d", &n)) {
    mp.clear();
    vec.clear();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    int res = a[1];
    for (int i = 2; i <= n; i++) res = gcd(res, a[i]);
    if (res != 1) {
      printf("-1\n");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      if (mp[a[i]] == 0) {
        mp[a[i]] = c[i];
        vec.push_back(a[i]);
      } else if (mp[a[i]] > c[i])
        mp[a[i]] = c[i];
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < vec.size(); j++) {
        int tmp = gcd(a[i], vec[j]);
        if (mp[tmp] == 0) {
          vec.push_back(tmp);
          mp[tmp] = mp[vec[j]] + mp[a[i]];
        } else if (mp[tmp] > mp[vec[j]] + mp[a[i]])
          mp[tmp] = mp[vec[j]] + mp[a[i]];
      }
    }
    printf("%d\n", mp[1]);
  }
  return 0;
}
