#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T Max(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
inline T Min(T a, T b) {
  return a < b ? a : b;
}
const double PI = acos(-1.0);
int N;
long long g;
pair<long long, long long> card[305];
map<long long, long long> mymap;
vector<pair<long long, long long> > dp[305];
long long gcd(long long a, long long b) { return b == 0ll ? a : gcd(b, a % b); }
int cmp(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second == b.second ? a.first < b.first : a.second < b.second;
}
void work() {
  for (int i = 1; i <= N; i++) {
    cin >> card[i].first;
    g = (i == 1) ? card[i].first : gcd(g, card[i].first);
  }
  for (int i = 1; i <= N; i++) {
    cin >> card[i].second;
  }
  sort(card + 1, card + 1 + N, cmp);
  if (g != 1) {
    cout << -1 << endl;
  } else {
    dp[1].push_back(card[1]);
    long long res = 1e9;
    if (card[1].first == 1) {
      res = Min(card[1].second, res);
    }
    for (int i = 2; i <= N; i++) {
      mymap.clear();
      for (int j = 0; j < dp[i - 1].size(); j++) {
        long long g = gcd(dp[i - 1][j].first, card[i].first);
        if (g == dp[i - 1][j].first) {
          continue;
        }
        if (mymap.find(g) == mymap.end()) {
          mymap.insert(make_pair(g, card[i].second + dp[i - 1][j].second));
        } else {
          mymap[g] = Min(mymap[g], card[i].second + dp[i - 1][j].second);
        }
      }
      for (int j = 0; j < dp[i - 1].size(); j++) {
        long long g = dp[i - 1][j].first;
        if (mymap.find(g) == mymap.end()) {
          mymap.insert(make_pair(g, dp[i - 1][j].second));
        } else {
          mymap[g] = Min(mymap[g], dp[i - 1][j].second);
        }
      }
      if (mymap.find(card[i].first) == mymap.end()) {
        mymap.insert(card[i]);
      } else {
        mymap[card[i].first] = Min(mymap[card[i].first], card[i].second);
      }
      for (map<long long, long long>::iterator it = mymap.begin();
           it != mymap.end(); it++) {
        if (it->first == 1) {
          res = Min(it->second, res);
        }
        dp[i].push_back(*it);
      }
    }
    cout << res << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  work();
  return 0;
}
