#include <bits/stdc++.h>
using namespace std;
const int MX = 3e2 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x3f3f3f3f3f3f3f3f;
int n, A[MX], B[MX], P[MX];
long long dp[1030];
long long solve() {
  long long ret = INF;
  for (int i = 1; i <= n; i++) {
    memset(dp, INF, sizeof(dp));
    int r = 0, t = A[i];
    dp[0] = B[i];
    for (int j = 2; (long long)j * j <= A[i]; j++) {
      if (t % j == 0) {
        P[r++] = j;
        while (t % j == 0) t /= j;
      }
    }
    if (t != 1) P[r++] = t;
    for (int j = 1; j <= n; j++) {
      int s = 0;
      for (int k = 0; k < r; k++) {
        if (A[j] % P[k]) s ^= 1 << k;
      }
      for (int k = 0; k < (1 << r); k++) {
        dp[k | s] = min(dp[k | s], dp[k] + B[j]);
      }
    }
    ret = min(ret, dp[(1 << r) - 1]);
  }
  return ret == INF ? -1 : ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &B[i]);
  printf("%I64d\n", solve());
  return 0;
}
