#include <bits/stdc++.h>
using namespace std;
long long mul(long long a, long long b) { return (a * b) % (100000007); }
long long add(long long a, long long b) { return (a + b) % (100000007); }
long long sub(long long a, long long b) {
  return (a - b + (a - b) / (100000007) * (100000007) + (100000007)) %
         (100000007);
}
priority_queue<int> q;
map<int, int> h;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int n, l[(300 + 300 + 10)], c[(300 + 300 + 10)];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &l[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    if (h.find(l[i]) != h.end()) {
      h[l[i]] = min(h[l[i]], c[i]);
      continue;
    }
    h[l[i]] = c[i], q.push(l[i]);
  }
  while (!q.empty()) {
    int t = q.top();
    q.pop();
    for (int i = 1; i <= n; i++) {
      int t2 = gcd(t, l[i]), c2 = h[t] + h[l[i]];
      if (h.find(t2) == h.end() || h[t2] > c2) {
        h[t2] = c2;
        q.push(t2);
      }
    }
  }
  if (h.find(1) == h.end())
    cout << "-1\n";
  else
    cout << h[1] << endl;
  return 0;
}
