#include <bits/stdc++.h>
using namespace std;
const int maxn = 350, maxns = 6000000, big = 0x3f3f3f3f;
int n, l[maxn], c[maxn], ans;
int num[maxns], ns;
int f[maxns];
void get(int x) {
  int i, lim = sqrt(x);
  num[ns++] = x;
  for (i = 2; i <= lim; i++)
    if (!(x % i)) num[ns++] = i, num[ns++] = x / i;
}
inline int gcd(int u, int v) { return !v ? u : gcd(v, u % v); }
int main() {
  int i, j, t;
  num[ns++] = 1;
  memset(f, big, sizeof(f));
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &l[i]), get(l[i]);
  sort(num, num + ns);
  ns = unique(num, num + ns) - num;
  for (i = 0; i < n; i++) {
    scanf("%d", &c[i]);
    t = lower_bound(num, num + ns, l[i]) - num;
    f[t] = min(f[t], c[i]);
    for (j = 0; j < ns; j++)
      if (f[j] != big) {
        if (num[j] == 143 && i == 4) int ttt = 1;
        t = lower_bound(num, num + ns, gcd(num[j], l[i])) - num;
        f[t] = min(f[t], f[j] + c[i]);
      }
  }
  ans = (f[0] == big ? -1 : f[0]);
  printf("%d\n", ans);
  return 0;
}
