#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[305];
void ReadData() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i].first;
  for (int i = 1; i <= n; i++) cin >> a[i].second;
}
vector<int> snt(int val) {
  vector<int> ans;
  for (int i = 2; i * i <= val; i++) {
    if (val % i == 0) {
      ans.push_back(i);
      while (val % i == 0) val /= i;
    }
  }
  if (val > 1) ans.push_back(val);
  return ans;
}
int dp[100000];
void process() {
  int res = 1e8;
  for (int i = 1; i <= n; i++) {
    vector<int> f = snt(a[i].first);
    vector<int> mask(n + 1, 0);
    for (int j = 1; j <= n; j++) {
      for (int k = 0; k < f.size(); k++) {
        if (a[j].first % f[k] == 0) mask[j] += (1 << k);
      }
    }
    int len = f.size();
    int MaxState = (1 << len) - 1;
    for (int j = 0; j <= MaxState; j++) dp[j] = 1e8;
    dp[mask[i]] = a[i].second;
    for (int j = 1; j <= n; j++) {
      for (int state = 0; state <= MaxState; state++) {
        int final_state = (state & mask[j]);
        dp[final_state] = min(dp[final_state], dp[state] + a[j].second);
      }
    }
    res = min(res, dp[0]);
  }
  if (res == 1e8)
    cout << -1;
  else
    cout << res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ReadData();
  process();
}
