#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, m, t, k;
int d[((int)2e5 + 5)];
int c[((int)2e5 + 5)];
map<int, int> mp;
int gcd(int a, int b) {
  int tmp;
  if (a > b) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a) {
    tmp = a;
    a = b % a;
    b = tmp;
  }
  return b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> d[i];
  for (int i = 0; i < n; ++i) cin >> c[i];
  for (int i = 0; i < n; ++i) {
    for (auto j : mp) {
      int gc = gcd(j.first, d[i]);
      if (mp.count(gc))
        mp[gc] = min(mp[gc], c[i] + j.second);
      else
        mp[gc] = c[i] + j.second;
    }
    if (mp.count(d[i]))
      mp[d[i]] = min(mp[d[i]], c[i]);
    else
      mp[d[i]] = c[i];
  }
  if (mp[1])
    cout << mp[1] << "\n";
  else
    cout << -1 << "\n";
  return 0;
}
