#include <bits/stdc++.h>
using namespace std;
struct Card {
  int len;
  int cost;
};
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  cin >> n;
  vector<Card> cards(n);
  for (Card &x : cards) cin >> x.len;
  for (Card &x : cards) cin >> x.cost;
  map<int, int> dp;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (auto x : dp) {
      int g = gcd(x.first, cards[i].len);
      if (dp.find(g) != dp.end()) {
        dp[g] = min(dp[g], x.second + cards[i].cost);
      } else
        dp[g] = x.second + cards[i].cost;
    }
  }
  auto aux = dp.find(1);
  if (aux == dp.end())
    cout << -1 << endl;
  else
    cout << (*aux).second << endl;
}
