#include <bits/stdc++.h>
using namespace std;
const int maxn = 310;
int l[maxn], c[maxn];
int n;
int f[1 << 9];
int g[maxn][10], cnt[maxn];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> l[i];
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    int x = l[i];
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        g[i][++cnt[i]] = j;
        while (x % j == 0) x /= j;
      }
    }
    if (x > 1) {
      g[i][++cnt[i]] = x;
    }
  }
  int ans = 1e9 + 10;
  for (int i = 1; i <= n; i++) {
    memset(f, 0x3f, sizeof f);
    int tot = (1 << cnt[i]) - 1;
    f[tot] = c[i];
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      int s = 0;
      for (int k = 1; k <= cnt[i]; k++) {
        int flag = 0;
        for (int x = 1; x <= cnt[j]; x++) {
          if (g[i][k] == g[j][x]) {
            flag = 1;
            break;
          }
        }
        if (flag) s |= (1 << (k - 1));
      }
      for (int k = 0; k <= tot; k++) f[k & s] = min(f[k & s], f[k] + c[j]);
    }
    ans = min(ans, f[0]);
  }
  if (ans == 1e9 + 10)
    cout << -1;
  else
    cout << ans;
}
