#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int inf = 1e9;
int n, l[maxn], c[maxn], ans, cnt, pri[maxn], lst, cur, p[maxn], st[maxn],
    f[maxn];
bool tf[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", l + i);
  for (int i = 1; i <= n; i++) scanf("%d", c + i);
  for (int i = 2; i <= 100000; i++) {
    if (!tf[i]) pri[++cnt] = i;
    for (int j = 1; j <= cnt && i * pri[j] <= 100000; j++) {
      tf[i * pri[j]] = true;
      if (i % pri[j] == 0) break;
    }
  }
  ans = inf;
  for (int i = 1; i <= n; i++) {
    lst = l[i];
    cur = 0;
    for (int j = 1; j <= cnt && pri[j] * pri[j] <= lst; j++)
      if (lst % pri[j] == 0) {
        p[++cur] = pri[j];
        while (lst % pri[j] == 0) lst /= pri[j];
      }
    if (lst != 1) p[++cur] = lst;
    for (int j = 1; j <= n; j++) {
      st[j] = 0;
      for (int k = 1; k <= cur; k++)
        if (l[j] % p[k] == 0) st[j] |= (1 << (k - 1));
    }
    for (int i = 0; i < (1 << 10); i++) f[i] = inf;
    f[(1 << cur) - 1] = c[i];
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < (1 << cur); j++)
        f[st[i] & j] = min(f[st[i] & j], f[j] + c[i]);
    ans = min(ans, f[0]);
  }
  if (ans >= inf)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
