#include <bits/stdc++.h>
using namespace std;
long long int n, a;
vector<long long int> val, card;
long long int m[305];
long long int dp[305][(1 << 9)];
long long int sizeo;
long long int rec(long long int wai, long long int mask) {
  if (__builtin_popcount(mask) == sizeo) return 0;
  if (wai == -1) return INT_MAX;
  if (dp[wai][mask] != -1) return dp[wai][mask];
  long long int gonnaret = INT_MAX;
  gonnaret = min(
      {gonnaret, rec(wai - 1, mask | m[wai]) + val[wai], rec(wai - 1, mask)});
  return dp[wai][mask] = gonnaret;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long int j = 0; j < 2; j++) {
    for (long long int i = 0; i < n; i++) {
      cin >> a;
      if (!j)
        card.push_back(a);
      else
        val.push_back(a);
    }
  }
  long long int ans = INT_MAX;
  for (long long int i = 0; i < n; i++) {
    vector<long long int> pfactors;
    long long int t = card[i];
    for (long long int j = 2; j * j <= t; j++) {
      if (!(t % j)) {
        pfactors.push_back(j);
        while (!(t % j)) t /= j;
      }
    }
    if (t > 1) pfactors.push_back(t);
    sizeo = pfactors.size();
    for (long long int j = 0; j < n; j++) {
      m[j] = 0;
      for (long long int ka = 0; ka < sizeo; ka++) {
        if ((card[j] % pfactors[ka])) m[j] |= (1 << ka);
      }
    }
    memset(dp, -1, sizeof dp);
    ans = min(ans, rec(n - 1, 0) + val[i]);
  }
  if (ans != INT_MAX)
    cout << ans << '\n';
  else
    cout << -1 << '\n';
}
