#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<long long, long long> > d[301];
vector<long long> l, c;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  cin >> n;
  l.assign(n, 0);
  c.assign(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  d[0].push_back({l[0], c[0]});
  for (int i = 1; i < n; i++) {
    d[i].push_back({l[i], c[i]});
    for (int j = 0; j < d[i - 1].size(); j++) {
      d[i].push_back({gcd(d[i - 1][j].first, l[i]), d[i - 1][j].second + c[i]});
    }
    for (int j = 0; j < d[i - 1].size(); j++) {
      d[i].push_back(d[i - 1][j]);
    }
    d[i - 1].clear();
    sort(d[i].begin(), d[i].end());
    for (int j = 1; j < d[i].size(); j++) {
      if (d[i][j - 1].first == d[i][j].first) {
        d[i][j - 1].second = min(d[i][j].second, d[i][j - 1].second);
        d[i][j].second = d[i][j - 1].second;
      }
    }
    d[i].erase(unique(d[i].begin(), d[i].end()), d[i].end());
  }
  long long answer = 1000LL * 1000LL * 1000LL * 1000LL;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < d[i].size(); j++) {
      if (d[i][j].first == 1) {
        answer = min(answer, d[i][j].second);
      }
    }
  }
  if (answer == 1000LL * 1000LL * 1000LL * 1000LL) {
    cout << -1;
  } else {
    cout << answer;
  }
  return 0;
}
