#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m[301], A[301], c[301], N, ans = INF;
int memo[301][1 << 9];
int sol(int cur, int mask) {
  if (cur == n + 1 && mask == (1 << N) - 1) return 0;
  if (cur == n + 1) return INF;
  if (memo[cur][mask] != -1) return memo[cur][mask];
  return memo[cur][mask] =
             min(sol(cur + 1, mask), sol(cur + 1, mask | m[cur]) + c[cur]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> A[i];
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    int k = A[i];
    vector<int> factors;
    for (int j = 2; j * j <= k; j++) {
      if (k % j == 0) {
        factors.push_back(j);
        while (k % j == 0) k /= j;
      }
    }
    if (k > 1) factors.push_back(k);
    N = factors.size();
    for (int j = 1; j <= n; j++) {
      m[j] = 0;
      for (int l = 0; l < N; l++)
        if (A[j] % factors[l] != 0) m[j] |= (1 << l);
    }
    memset(memo, -1, sizeof(memo));
    ans = min(ans, sol(1, 0) + c[i]);
  }
  if (ans != INF)
    cout << ans;
  else
    cout << -1;
  return 0;
}
