#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T x, T y) {
  if (!y) return x;
  return gcd(y, x % y);
}
template <class T>
inline T mod(T x) {
  if (x < 0)
    return -x;
  else
    return x;
}
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int MAXN = 301;
const int MAX = (1 << 9) + 5;
int dp[MAXN][MAX];
int l[MAXN], cost[MAXN], m[MAX], n, N;
int recur(int cur, int mask) {
  if (cur == n) {
    if (mask == (1 << N) - 1)
      return 0;
    else
      return INF;
  }
  if (dp[cur][mask] != -1) return dp[cur][mask];
  dp[cur][mask] = INF;
  dp[cur][mask] = min(dp[cur][mask], recur(cur + 1, mask));
  dp[cur][mask] = min(dp[cur][mask], recur(cur + 1, mask | m[cur]) + cost[cur]);
  return dp[cur][mask];
}
int main(int argc, char* argv[]) {
  if (argc == 2 or argc == 3) freopen(argv[1], "r", stdin);
  if (argc == 3) freopen(argv[2], "w", stdout);
  ios::sync_with_stdio(false);
  int ans;
  cin >> n;
  for (__typeof(0) i = 0; i < n; i += 1) {
    cin >> l[i];
  }
  for (__typeof(0) i = 0; i < n; i += 1) {
    cin >> cost[i];
  }
  ans = INF;
  for (__typeof(0) i = 0; i < n; i += 1) {
    int x = l[i];
    vector<int> factor;
    for (__typeof(2LL) j = 2LL; j < x; j += 1) {
      if (j * j > x) break;
      if (x % j == 0) factor.push_back(j);
      while (x % j == 0) x /= j;
    }
    if (x > 1) factor.push_back(x);
    N = factor.size();
    for (__typeof(0) j = 0; j < n; j += 1) {
      m[j] = 0;
      for (__typeof(0) k = 0; k < N; k += 1) {
        if (l[j] % factor[k]) m[j] |= (1 << k);
      }
    }
    for (__typeof(0) k = 0; k < MAXN; k += 1)
      for (__typeof(0) p = 0; p < MAX; p += 1) dp[k][p] = -1;
    ans = min(ans, cost[i] + recur(0, 0));
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
