#include <bits/stdc++.h>
using namespace std;
vector<int> primes, factors[302];
int cost[302];
int s[1000002];
void run_sieve() {
  for (int i = 2; i <= 1000000; i++) {
    if (s[i]) continue;
    for (int j = i + i; j <= 1000000; j += i) {
      s[j] = i;
    }
  }
  for (int i = 2; i <= 1000000; i++) {
    if (s[i] == 0) primes.push_back(i);
  }
}
int a[301], mask_size, n, dp[301][(1 << 11)], idxInContext;
void factorize(int idx) {
  int x = a[idx];
  for (int i = 0; i < primes.size(); i++) {
    if (x % primes[i] == 0) {
      factors[idx].push_back(primes[i]);
      while (x % primes[i] == 0) x /= primes[i];
    }
  }
  if (x > 1) factors[idx].push_back(x);
}
int func(int idx, int mask) {
  if (mask == 0) return 0;
  if (idx > n) return 1000000007;
  if (dp[idx][mask] != -1) return dp[idx][mask];
  int ans = func(idx + 1, mask);
  int nmask = mask;
  for (int i = 0; i < mask_size; i++) {
    if ((1 << i) & mask) {
      if (a[idx] % factors[idxInContext][i] != 0) {
        nmask -= (1 << i);
      }
    }
  }
  ans = min(ans, func(idx + 1, nmask) + cost[idx]);
  return dp[idx][mask] = ans;
}
int main() {
  run_sieve();
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    factorize(i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &cost[i]);
  }
  int ans = 1000000007;
  for (int i = 1; i <= n; i++) {
    mask_size = factors[i].size();
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= (1 << mask_size); k++) {
        dp[j][k] = -1;
      }
    }
    idxInContext = i;
    ans = min(ans, cost[i] + func(i + 1, (1 << mask_size) - 1));
  }
  if (ans == 1000000007)
    cout << "-1\n";
  else
    cout << ans << endl;
  return 0;
}
