#include <bits/stdc++.h>
using namespace std;
const int MAXN = 310;
vector<int> L, C;
const int INF = 1 << 29;
int dp[MAXN][1 << 9];
int main() {
  int n;
  scanf("%d", &n);
  L.resize(n);
  C.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &L[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &C[i]);
  }
  int ans = INF;
  for (int i = 0; i < n; i++) {
    if (L[i] == 1) {
      ans = min(C[i], ans);
      continue;
    }
    int x = L[i];
    vector<int> facts;
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        facts.push_back(j);
        while (x % j == 0) {
          x /= j;
        }
      }
    }
    if (x != 1) {
      facts.push_back(x);
    }
    int m = facts.size();
    int sl = L[i], sc = C[i];
    fill(dp[0], dp[0] + (1 << m), INF);
    dp[0][0] = 0;
    for (int j = 0; j < n; j++) {
      fill(dp[j + 1], dp[j + 1] + (1 << m), INF);
      for (int k = 0; k < (1 << m); k++) {
        if (dp[j][k] == INF) continue;
        dp[j + 1][k] = min(dp[j + 1][k], dp[j][k]);
        int nk = k;
        for (int a = 0; a < m; a++) {
          if (L[j] % facts[a] != 0) {
            nk |= (1 << a);
          }
        }
        dp[j + 1][nk] = min(dp[j + 1][nk], dp[j][k] + C[j]);
      }
    }
    ans = min(ans, sc + dp[n][(1 << m) - 1]);
  }
  if (ans < INF)
    printf("%d\n", ans);
  else
    printf("-1\n");
  return 0;
}
