#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> l, c;
map<int, int> dp;
int gcd(int first, int second) {
  if (first == 0) return second;
  return gcd(second % first, first);
}
int main() {
  cin >> n;
  for (int i = (0); i < (int)(n); i++) {
    int t;
    cin >> t;
    l.push_back(t);
  }
  for (int i = (0); i < (int)(n); i++) {
    int t;
    cin >> t;
    c.push_back(t);
  }
  for (int i = (0); i < (int)(n); i++) {
    if (dp.find(l[i]) == dp.end())
      dp[l[i]] = c[i];
    else
      dp[l[i]] = min(dp[l[i]], c[i]);
    for (auto &it : dp) {
      int g = gcd(it.first, l[i]);
      if (dp.find(g) == dp.end())
        dp[g] = it.second + c[i];
      else
        dp[g] = min(dp[g], it.second + c[i]);
    }
  }
  int ans = (dp.find(1) == dp.end() ? -1 : dp[1]);
  cout << ans << endl;
  return 0;
}
