#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int l = 0;
int a[10005], f[40005], pr[10005], p[30], d[10005], v[10005], b[10005],
    dp[10005];
void pre() {
  int i, j;
  long long now = 1;
  for (i = 2; i <= 40000; i++)
    if (!f[i]) {
      pr[++l] = i;
      for (j = i + i; j <= 40000; j += i) f[j] = 1;
    }
  for (i = 1; i <= 22; i++) p[i] = 1 << (i - 1);
}
int find(int x, int ll, int rr) {
  int i, j, now, op = 0;
  for (i = 1; i <= l; i++) {
    if (x % pr[i] == 0) {
      b[++op] = pr[i];
      while (x % pr[i] == 0) x /= pr[i];
    }
  }
  if (x > 1) b[++op] = x;
  for (i = ll; i <= rr; i++) {
    now = 0;
    for (j = 1; j <= op; j++)
      if (a[i] % b[j] == 0) now |= p[j];
    d[i] = now;
  }
  return op;
}
int main() {
  int n, i, j, k, l = 0, inf, ans = INT_MAX;
  pre();
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (i = 1; i <= n; i++) {
    l = find(a[i], i, n);
    memset(dp, 0x3f, sizeof(dp));
    inf = dp[0];
    dp[p[l + 1] - 1] = v[i];
    for (j = i; j <= n; j++) {
      for (k = p[l + 1] - 1; k >= 1; k--) {
        dp[k & d[j]] = min(dp[k & d[j]], dp[k] + v[j]);
      }
    }
    ans = min(ans, dp[0]);
  }
  if (ans == inf) ans = -1;
  printf("%d\n", ans);
  return 0;
}
