#include <bits/stdc++.h>
using namespace std;
const int inf = (int)(1e9);
int n;
int l[300];
int c[300];
int m[300];
int d[1 << 10];
vector<int> q[300];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); i++) scanf("%d", &l[i]);
  for (int i = 0; i < (int)(n); i++) scanf("%d", &c[i]);
  for (int i = 0; i < (int)(n); i++) {
    int x = l[i];
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) q[i].push_back(j);
      while (x % j == 0) {
        x /= j;
      }
    }
    if (x != 1) q[i].push_back(x);
  }
  int res = inf;
  for (int i = 0; i < (int)(n); i++) {
    int mmask = (1 << q[i].size()) - 1;
    d[mmask] = c[i];
    for (int mask = 0; mask < (int)(mmask); mask++) {
      d[mask] = inf;
    }
    for (int j = 0; j < (int)(n); j++) {
      m[j] = 0;
      for (int k = 0; k < (int)(q[i].size()); k++)
        m[j] |= ((l[j] % q[i][k] == 0) << k);
    }
    for (int j = 0; j < (int)(n); j++) {
      if (i != j) {
        for (int mask = 0; mask < (int)(mmask + 1); mask++) {
          d[mask & m[j]] = min(d[mask & m[j]], d[mask] + c[j]);
        }
      }
    }
    res = min(res, d[0]);
  }
  if (res != inf)
    cout << res << endl;
  else
    cout << -1 << endl;
}
