#include <bits/stdc++.h>
using namespace std;
void setIO(string str, bool dbg) {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  cout.tie(nullptr);
  if (!dbg) {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
const int MAX = 1e5 + 5;
const int LEN = 6e1 + 5;
const int LVL = 2e1 + 0;
const long long int MOD = 1e9 + 7;
const long long int INF = 2e9;
int xyz = 1;
int n;
int ls[MAX];
int cs[MAX];
int gcd(int a, int b) { return !a ? b : gcd(b % a, a); }
void fnc() {
  cin >> n;
  map<int, int> dps;
  dps[0] = 0;
  for (int i = 1; i <= n; i++) cin >> ls[i];
  for (int i = 1; i <= n; i++) cin >> cs[i];
  for (int i = 1; i <= n; i++) {
    int l = ls[i];
    int c = cs[i];
    for (auto ele : dps) {
      int p = gcd(ele.first, l);
      int v = dps.count(p) ? dps[p] : INT_MAX;
      dps[p] = min(v, ele.second + c);
    }
  }
  cout << (dps.count(1) ? dps[1] : -1) << "\n";
}
int main() {
  setIO("", 1);
  while (xyz--) fnc();
  return 0;
}
