#include <bits/stdc++.h>
using namespace std;
long long a[1000];
long long c[1000];
map<long long, long long> m[301];
long long gcd(long long a, long long b) {
  if (a * b > 0)
    gcd(b % a, a);
  else
    return a + b;
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> c[i];
  m[0][a[0]] = c[0];
  m[0][0] = 0;
  for (long long i = 1; i <= n; i++)
    for (map<long long, long long>::iterator it = m[i - 1].begin();
         it != m[i - 1].end(); it++) {
      if (!(m[i].count(it->first))) {
        m[i][it->first] = m[i - 1][it->first];
      } else {
        m[i][it->first] = min(m[i][it->first], m[i - 1][it->first]);
      }
      if (!(m[i].count(gcd(it->first, a[i]))))
        m[i][gcd(it->first, a[i])] = m[i - 1][it->first] + c[i];
      else
        m[i][gcd(it->first, a[i])] = min(m[i - 1][it->first] + c[i],
                                         (long long)m[i][gcd(it->first, a[i])]);
    }
  if (!(m[n].count(1)))
    cout << -1;
  else
    cout << m[n][1] << endl;
}
