#include <bits/stdc++.h>
using namespace std;
int a[400], c[400];
map<int, int> b, d;
int gcd(int a, int b) {
  int c;
  while (a != 0) {
    c = a;
    a = b % a;
    b = c;
  }
  return b;
}
int main() {
  int i, j, k, N, x, y, z, cost = 1e9;
  scanf("%d", &N);
  for (i = 0; i < N; ++i) {
    scanf("%d", &x);
    a[i] = x;
  }
  for (i = 0; i < N; ++i) {
    scanf("%d", &y);
    c[i] = y;
  }
  if (N == 1) {
    if (x == 1)
      printf("%d\n", y);
    else
      printf("-1\n");
    return 0;
  }
  d[a[0]] = c[0];
  b[a[0]] = c[0];
  for (i = 1; i < N; ++i) {
    for (std::map<int, int>::iterator it = d.begin(); it != d.end(); it++) {
      x = (it)->first;
      y = (it)->second;
      z = gcd(x, a[i]);
      if (b.count(z)) {
        if (b[z] > y + c[i]) b[z] = y + c[i];
      } else
        b[z] = y + c[i];
    }
    z = a[i];
    if (b.count(z)) {
      if (b[z] > c[i]) b[z] = c[i];
    } else
      b[z] = c[i];
    d = b;
  }
  if (b.count(1)) {
    printf("%d\n", b[1]);
  } else
    printf("-1\n");
  return 0;
}
