#include <bits/stdc++.h>
using namespace std;
const long long inf = 10000000000LL;
long long ara[305], bara[305], dp[305][515], pri[305][305];
vector<long long> vec[305];
long long n, f;
long long cnt(long long pos, long long mask) {
  if (pos == n + 1) return mask ? inf : 0;
  if (dp[pos][mask] != -1) return dp[pos][mask];
  return dp[pos][mask] = min(cnt(pos + 1, mask),
                             bara[pos] + cnt(pos + 1, mask & pri[f][pos]));
}
int main() {
  long long i, j, k, p, q, x, y;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> ara[i];
  for (i = 1; i <= n; i++) cin >> bara[i];
  for (i = 1; i <= n; i++) {
    for (j = 2; j * j <= ara[i]; j++) {
      if (ara[i] % j == 0) {
        while (ara[i] % j == 0) ara[i] /= j;
        vec[i].push_back(j);
      }
    }
    if (ara[i] != 1) vec[i].push_back(ara[i]);
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      for (k = 0; k < vec[i].size(); k++) {
        if (find(vec[j].begin(), vec[j].end(), vec[i][k]) != vec[j].end())
          pri[i][j] |= (1 << k);
      }
    }
  }
  x = inf;
  for (f = 1; f <= n; f++) {
    memset(dp, -1, sizeof(dp));
    x = min(x, bara[f] + cnt(f + 1, pri[f][f]));
  }
  if (x >= inf) x = -1;
  cout << x << endl;
  return 0;
}
