#include <bits/stdc++.h>
using namespace std;
set<int> s;
priority_queue<pair<long long, int> > q;
int n, l[1234567];
long long c[1234567];
vector<int> cur;
int gcd(int a, int b) {
  if (a > b) swap(a, b);
  if (!a) return b;
  return gcd(b % a, a);
}
int main() {
  scanf("%d", &n);
  for (int i = (0); i < (n); i++) scanf("%d", l + i);
  for (int i = (0); i < (n); i++) scanf("%lld", c + i);
  for (int i = (0); i < (n); i++) q.push({-c[i], l[i]});
  while (!q.empty()) {
    long long cst = -q.top().first;
    int at = q.top().second;
    q.pop();
    if (s.find(at) != s.end()) continue;
    s.insert(at);
    if (at == 1) {
      printf("%lld\n", cst);
      return 0;
    }
    for (int i = (0); i < (cur.size()); i++) {
      int lk = cur[i];
      int nw = gcd(l[lk], at);
      long long nwcst = cst + c[lk];
      q.push({-nwcst, nw});
    }
    l[n] = at;
    c[n] = cst;
    cur.push_back(n);
    n++;
  }
  puts("-1");
  return 0;
}
