#include <bits/stdc++.h>
using namespace std;
map<long long, int> map1;
long long arr[302];
long long cost[302];
long long dp[301][1 << 9];
int mask[302];
vector<int> factors[302];
long long n, N;
long long min(long long x, long long y) {
  if (x <= y)
    return x;
  else
    return y;
}
int solve(int x, int y) {
  if (x == n + 1) {
    if (y == (1 << factors[N].size()) - 1) {
      return 0;
    } else {
      return 1000000000;
    }
  }
  if (dp[x][y] != -1) {
    return dp[x][y];
  }
  int ret = 1000000000;
  ret = min(1000000000, solve(x + 1, y));
  ret = min(ret, cost[x] + solve(x + 1, mask[x] | y));
  dp[x][y] = ret;
  return ret;
}
int main() {
  long long i, j, k, l, m, t;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> arr[i];
    m = arr[i];
    for (j = 2; j * j <= arr[i]; j++) {
      if (m % j == 0) {
        factors[i].push_back(j);
        while (m % j == 0) {
          m /= j;
        }
      }
    }
    if (m != 1) factors[i].push_back(m);
  }
  for (i = 1; i <= n; i++) cin >> cost[i];
  long long ans = 1000000000LL;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      mask[j] = 0;
      for (k = 0; k < factors[i].size(); k++) {
        if (arr[j] % factors[i][k] != 0) mask[j] |= (1 << k);
      }
    }
    for (j = 1; j <= n; j++) {
      for (k = 0; k <= 1 << factors[i].size(); k++) dp[j][k] = -1;
    }
    m = cost[i];
    N = i;
    m += solve(i, 0);
    N = factors[i].size();
    ans = min(ans, m);
  }
  if (ans != 1000000000)
    cout << ans << endl;
  else
    cout << "-1\n";
}
