#include <bits/stdc++.h>
int gsd(int y, int h) {
  if (h == 0) return y;
  if (y == 0) return h;
  if (h > y) {
    return gsd(h % y, y);
  } else {
    return gsd(y % h, h);
  }
}
class card {
  long l;
  long c;

 public:
  void putlc(int ll, int cc) {
    l = ll;
    c = cc;
  }
  void putc(int cc) { c = cc; }
  int getl() { return l; }
  int getc() { return c; }
  bool operator>(card f) { return (l > f.l); }
  card plus(card t) {
    card ans;
    if (gsd(l, t.l) == l) {
      ans = *this;
    } else {
      ans.putlc(gsd(l, t.l), (c + t.c));
    }
    return ans;
  }
};
void sort(card* m, int dl) {
  if (dl < 4) {
    for (int i = 0; i < dl; i++)
      for (int j = 0; j < dl - 1; j++) {
        if (m[j] > m[j + 1]) {
          card t = m[j];
          m[j] = m[j + 1];
          m[j + 1] = t;
        }
      }
    return;
  }
  card* m1 = m;
  int dl1 = dl / 2;
  int dl2 = (dl - dl1);
  card* m2 = &m[dl1];
  sort(m1, dl1);
  sort(m2, dl2);
  int y1 = 0;
  int y2 = 0;
  int y = 0;
  card* nm = new card[dl];
  while (y < dl) {
    if (y1 == dl1) {
      nm[y] = m2[y2];
      y++;
      y2++;
      continue;
    }
    if (y2 == dl2) {
      nm[y] = m1[y1];
      y++;
      y1++;
      continue;
    }
    if (m1[y1] > m2[y2]) {
      nm[y] = m2[y2];
      y++;
      y2++;
    } else {
      nm[y] = m1[y1];
      y++;
      y1++;
    }
  }
  y = 0;
  for (y = 0; y < dl; y++) {
    m[y] = nm[y];
  }
  delete[] nm;
  return;
}
void norm(card* m, int* dl) {
  int dll = *dl;
  card* nm = new card[dll];
  int y = 1;
  nm[0] = m[0];
  for (int i = 1; i < dll; i++) {
    if (nm[y - 1].getl() != m[i].getl()) {
      nm[y] = m[i];
      y++;
      continue;
    }
    if (m[i].getc() < nm[y - 1].getc()) {
      nm[y - 1].putc(m[i].getc());
    }
  }
  for (int i = 0; i < y; i++) {
    m[i] = nm[i];
  }
  delete[] nm;
  *dl = y;
  return;
}
int main() {
  int ans = 10000000;
  card base[300];
  int n;
  scanf("%d", &n);
  {
    int l[300];
    int c[300];
    for (int i = 0; i < n; i++) {
      scanf("%d", &l[i]);
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &c[i]);
    }
    for (int i = 0; i < n; i++) {
      if ((l[i] == 1) && (c[i] < ans)) {
        ans = c[i];
      }
    }
    for (int i = 0; i < n; i++) {
      base[i].putlc(l[i], c[i]);
    }
    int D = l[0];
    for (int i = 0; i < n; i++) D = gsd(D, l[i]);
    if (D != 1) {
      printf("-1");
      return 0;
    }
  }
  card* this_level = new card[3000000];
  int tl = 0;
  card* next_level = new card[3000000];
  int nl = 0;
  {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        this_level[tl] = base[i].plus(base[j]);
        tl++;
      }
    sort(this_level, tl);
    norm(this_level, &tl);
  }
  for (int hjk = 0; hjk < 6; hjk++) {
    nl = 0;
    for (int i = 0; i < tl; i++)
      for (int j = 0; j < n; j++) {
        next_level[nl] = this_level[i].plus(base[j]);
        nl++;
      }
    sort(next_level, nl);
    norm(next_level, &nl);
    tl = nl;
    for (int i = 0; i < tl; i++) {
      this_level[i] = next_level[i];
    }
  }
  if (ans > this_level[0].getc()) {
    ans = this_level[0].getc();
  }
  printf("%d", ans);
  return 0;
}
