#include <bits/stdc++.h>
using namespace std;
class bor {
 public:
  int q;
  bor *a[2];
  bor() {
    q = -1;
    a[0] = a[1] = 0;
  }
};
int y = 0, *d = new int[1500001], *f = new int[1500001];
bor *c = new bor;
int l(int w) {
  int e = w;
  bor *v = c;
  do {
    if (v->a[w & 1] == 0) v->a[w & 1] = new bor;
    v = v->a[w & 1];
    w >>= 1;
  } while (w);
  if (v->q < 0) {
    v->q = y;
    d[y] = 1 << 30;
    f[y] = e;
    y++;
  }
  return v->q;
}
int k(int e, int r) {
  while ((e) && (r))
    if (e > r)
      e %= r;
    else
      r %= e;
  return e + r;
}
int main() {
  int q, w, e, r, t, a[300], s[300];
  cin >> q;
  for (w = 0; w < q; w++) cin >> a[w];
  for (w = 0; w < q; w++) cin >> s[w];
  l(0);
  d[0] = 0;
  for (w = 0; w < q; w++)
    for (e = y - 1; e >= 0; e--) {
      r = l(k(a[w], f[e]));
      d[r] = min(d[r], d[e] + s[w]);
    }
  r = l(1);
  if (d[r] == 1 << 30)
    cout << -1;
  else
    cout << d[r];
  return 0;
}
