#include <bits/stdc++.h>
using namespace std;
int dp[320][520];
vector<int> p[320];
int a[320], c[320];
int n;
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
void mk(int ii, int tmp) {
  int i, j;
  for (i = 2; i * i <= a[ii]; i += 1) {
    if (tmp % i == 0) p[ii].push_back(i);
    while (tmp % i == 0) tmp /= i;
  }
  if (tmp != 1) p[ii].push_back(tmp);
}
int mkn(int jj, int k) {
  int i, j, res = 1;
  j = 0;
  while (k) {
    if (k % 2 == 1) res *= p[jj][j];
    k /= 2;
    j++;
  }
  return res;
}
int mkk(int ii, int k) {
  int i, j, sz = p[ii].size(), res = 0;
  j = 0;
  for (i = sz - 1; i >= 0; i--) {
    res *= 2;
    if (k % p[ii][i] == 0) res += 1;
  }
  return res;
}
int main() {
  int i, j, k;
  int sz1, sz2;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      p[i].clear();
      mk(i, a[i]);
    }
    for (i = 1; i <= n; i++) scanf("%d", &c[i]);
    for (i = 1; i <= n; i++)
      for (j = 0; j <= 520; j++) dp[i][j] = 1e9;
    for (i = 1; i <= n; i++) {
      sz1 = p[i].size();
      dp[i][(1 << sz1) - 1] = min(dp[i][(1 << sz1) - 1], c[i]);
      for (j = 1; j <= i - 1; j++) {
        sz2 = p[j].size();
        for (k = 0; k < (1 << sz2); k++) {
          int x = mkn(j, k);
          x = gcd(a[i], x);
          x = mkk(i, x);
          dp[i][x] = min(dp[i][x], dp[j][k] + c[i]);
        }
      }
    }
    int ans = 1e9;
    for (i = 1; i <= n; i++) ans = min(dp[i][0], ans);
    if (ans >= 1e9)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
