#include <bits/stdc++.h>
using namespace std;
int l[5005], c[5005], n;
int gcd(int x, int y) { return (y == 0 ? x : gcd(y, x % y)); }
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int j = 0; j < n; j++) cin >> c[j];
  map<int, int> first;
  first[0] = 0;
  map<int, int>::iterator it;
  for (int i = 0; i < n; i++) {
    for (it = first.begin(); it != first.end(); it++) {
      int g = gcd(l[i], it->first);
      if (!first.count(g))
        first[g] = c[i] + it->second;
      else
        first[g] = min(first[g], c[i] + it->second);
    }
  }
  if (first.count(1))
    cout << first[1];
  else
    cout << -1;
  return 0;
}
