#include <bits/stdc++.h>
using namespace std;
const int MAXN = 350;
const int INF = (int)1e9;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool isPrime(int x) {
  for (int i = 2; i * i <= x; i++)
    if (x % i == 0) return false;
  return true;
}
int n;
int a[MAXN];
int c[MAXN];
int pn;
int p[MAXN];
int ans;
int dp[MAXN][1 << 15];
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", &c[i]);
    ans += c[i];
  }
  if (n == 1) {
    if (a[1] == 1) {
      printf("%d\n", c[1]);
    } else
      puts("-1");
    return 0;
  }
  int g = a[1];
  for (int i = 2; i <= n; i++) g = gcd(g, a[i]);
  if (g != 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int cur = a[i];
    if (cur == 1) {
      ans = min(ans, c[i]);
      continue;
    }
    pn = 0;
    for (int j = 2; j * j <= cur; j++) {
      if (cur % j == 0) {
        pn++;
        p[pn] = j;
        while (cur % j == 0) cur /= j;
      }
    }
    if (cur > 1) {
      pn++;
      p[pn] = cur;
    }
    for (int j = i; j <= n; j++) {
      for (int k = 0; k < (1 << pn); k++) dp[j][k] = INF;
    }
    dp[i][(1 << pn) - 1] = c[i];
    for (int j = i; j <= n; j++) {
      if (dp[j][0] < ans) ans = dp[j][0];
      int mask = 0;
      for (int k = 1; k <= pn; k++)
        if ((a[j] % p[k]) == 0) mask |= (1 << (k - 1));
      for (int k = 0; k < (1 << pn); k++) {
        if (dp[j][k] == INF) continue;
        if (j + 1 <= n) {
          if (dp[j][k] < dp[j + 1][k]) dp[j + 1][k] = dp[j][k];
          int gomask = k & mask;
          if (dp[j][k] + c[j] < dp[j + 1][gomask])
            dp[j + 1][gomask] = dp[j][k] + c[j];
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
