#include <bits/stdc++.h>
int v[300], c[300], t[10000000], u[10000000], lista[666019], next[10000000];
inline int gcd(int a, int b) {
  int r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int main() {
  int n, i, k, j, ans, y, x, p;
  fscanf(stdin, "%d", &n);
  for (i = 0; i < n; i++) {
    fscanf(stdin, "%d", &v[i]);
  }
  for (i = 0; i < n; i++) {
    fscanf(stdin, "%d", &c[i]);
  }
  t[1] = 0;
  u[1] = 0;
  k = 1;
  for (i = 0; i < n; i++) {
    y = k;
    for (j = 1; j <= k; j++) {
      x = gcd(v[i], t[j]);
      p = lista[x % 666019];
      while ((p) && (t[p] != x)) {
        p = next[p];
      }
      if (p) {
        if (u[p] > u[j] + c[i]) {
          u[p] = u[j] + c[i];
        }
      } else {
        y++;
        u[y] = u[j] + c[i];
        t[y] = x;
        next[y] = lista[x % 666019];
        lista[x % 666019] = y;
      }
    }
    k = y;
  }
  ans = 1000000000;
  for (i = 1; i <= k; i++) {
    if ((t[i] == 1) && (ans > u[i])) {
      ans = u[i];
    }
  }
  if (ans == 1000000000) {
    ans = -1;
  }
  fprintf(stdout, "%d\n", ans);
  fclose(stdin);
  fclose(stdout);
  return 0;
}
