#include <bits/stdc++.h>
using namespace std;
map<long long, long long> m;
long long c[400], l[400];
long long gcd(long long a, long long b) {
  long long r = a % b;
  while (r) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main() {
  long long n, i, mingcd = 2, current, cost = LLONG_MAX;
  map<long long, long long>::iterator iter;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> l[i];
  }
  for (i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (i = 0; i < n; i++) {
    for (map<long long, long long>::iterator it = m.begin(); it != m.end();
         ++it) {
      if (it->first != 1) {
        current = gcd(it->first, l[i]);
        iter = m.find(current);
        if (iter == m.end()) {
          m.insert(make_pair(current, it->second + c[i]));
        } else {
          if (iter->second > it->second + c[i]) {
            m[current] = it->second + c[i];
          }
        }
        if (current == 1 && it->second + c[i] < cost) {
          mingcd = 1;
          cost = it->second + c[i];
        }
      }
    }
    iter = m.find(l[i]);
    if (iter == m.end()) {
      m.insert(make_pair(l[i], c[i]));
    } else {
      if (iter->second > c[i]) {
        m[l[i]] = c[i];
      }
    }
    if (l[i] == 1 && c[i] < cost) {
      mingcd = 1;
      cost = c[i];
    }
  }
  if (mingcd == 2)
    cout << "-1\n";
  else
    cout << cost << "\n";
  return 0;
}
