#include <bits/stdc++.h>
using namespace std;
int l[310], c[310];
vector<int> p[310];
int dp[310][(1 << 9)];
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int fun(int j, int k) {
  int res = 1;
  int id = 0;
  while (k) {
    if (k & 1) {
      res *= p[j][id];
    }
    k = k >> 1;
    id++;
  }
  return res;
}
int fun1(int i, int tmp) {
  int len = p[i].size();
  int res = 0;
  for (int j = len - 1; j >= 0; j--) {
    res = res * 2;
    if (tmp % p[i][j] == 0) {
      res += 1;
    }
  }
  return res;
}
void make(int i) {
  int tmp = l[i];
  for (int j = 2; j * j <= l[i]; j++) {
    if (tmp % j == 0) {
      while (tmp % j == 0) {
        tmp = tmp / j;
      }
      p[i].push_back(j);
    }
  }
  if (tmp > 1) p[i].push_back(tmp);
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &l[i]);
      p[i].clear();
    }
    for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
    for (int i = 1; i <= n; i++) make(i);
    for (int i = 0; i <= n; i++)
      for (int j = 0; j < (1 << 9); j++) dp[i][j] = 0x3fffffff;
    for (int i = 1; i <= n; i++) {
      int t1 = p[i].size();
      dp[i][(1 << t1) - 1] = min(dp[i][(1 << t1) - 1], c[i]);
      for (int j = 1; j < i; j++) {
        int t2 = p[j].size();
        for (int k = 0; k < ((1 << t2)); k++) {
          int tmp = fun(j, k);
          tmp = gcd(l[i], tmp);
          int x = fun1(i, tmp);
          dp[i][x] = min(dp[i][x], dp[j][k] + c[i]);
        }
      }
    }
    int ans = 0x3fffffff;
    for (int i = 1; i <= n; i++) ans = min(dp[i][0], ans);
    if (ans >= 0x3fffffff)
      printf("-1\n");
    else
      printf("%d\n", ans);
  }
  return 0;
}
