#include <bits/stdc++.h>
using namespace std;
map<long long, long long> dp;
vector<pair<long long, long long> > input;
long long gcd(long long a, long long b) {
  if (a < b) return gcd(b, a);
  if (a == 0) return 0;
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int N, a, c;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> a;
    input.push_back(make_pair(a, 0));
  }
  for (int i = 0; i < N; i++) {
    cin >> c;
    input[i].second = c;
  }
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (auto pairs : dp) {
      long long g = gcd(input[i].first, pairs.first);
      if (dp.find(g) == dp.end())
        dp[g] = pairs.second + input[i].second;
      else if (dp.find(g) != dp.end())
        dp[g] = min(dp[g], pairs.second + input[i].second);
    }
  }
  if (dp.find(1) == dp.end()) {
    cout << "-1" << endl;
  } else {
    cout << dp[1] << endl;
  }
  return 0;
}
