#include <bits/stdc++.h>
using namespace std;
map<int, int> s[2];
int k = 0;
int n, c[500], l[500];
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int go() {
  int i;
  map<int, int>::iterator it;
  s[0][l[0]] = c[0];
  {}
  for (i = 0; i < n - 1; i++) {
    s[(i + 1) % 2].clear();
    for (it = s[i % 2].begin(); it != s[i % 2].end(); it++) {
      int cc = it->second;
      int ll = it->first;
      if (s[(i + 1) % 2].count(ll) == 0) {
        s[(i + 1) % 2][ll] = cc;
      } else
        s[(i + 1) % 2][ll] = min(s[i % 2][ll], cc);
      int lll = gcd(ll, l[i + 1]);
      int ccc = cc + c[i + 1];
      if (s[(i + 1) % 2].count(lll) == 0)
        s[(i + 1) % 2][lll] = ccc;
      else
        s[(i + 1) % 2][lll] = min(s[(i + 1) % 2][lll], ccc);
    }
    if (s[(i + 1) % 2].count(l[i + 1]) == 0)
      s[(i + 1) % 2][l[i + 1]] = c[i + 1];
    else
      s[(i + 1) % 2][l[i + 1]] = min(s[(i + 1) % 2][l[i + 1]], c[i + 1]);
    {}
  }
  if (s[i % 2].count(1) == 0) {
    return -1;
  } else
    return s[i % 2][1];
}
int main() {
  int i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d", &l[i]);
  for (i = 0; i < n; i++) scanf("%d", &c[i]);
  printf("%d\n", go());
  return 0;
}
