#include <bits/stdc++.h>
using namespace std;
int n, a[1111], b[1111];
const int inf = 1e9;
map<int, int> mp, mp1;
int nsd(int x, int y) {
  while (x > 0 && y > 0)
    if (x > y)
      x %= y;
    else
      y %= x;
  return x + y;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  mp[a[1]] = b[1];
  for (int i = 2; i <= n; i++) {
    for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
      int heh = it->first;
      int kok = it->second;
      if (!mp1[heh] || mp1[heh] > kok) mp1[heh] = kok;
      int kek = nsd(a[i], heh);
      if (!mp1[kek] || mp1[kek] > kok + b[i]) mp1[kek] = kok + b[i];
    }
    mp = mp1;
    mp1.clear();
    if (!mp[a[i]] || mp[a[i]] > b[i]) mp[a[i]] = b[i];
  }
  if (mp[1] == 0)
    cout << -1;
  else
    cout << mp[1];
}
