#include <bits/stdc++.h>
using namespace std;
int n;
int l[305];
int c[305];
int gcd(int a, int b) {
  if (a < b) {
    int t = a;
    a = b;
    b = t;
  }
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  map<int, int> m1;
  map<int, int> m2;
  for (int i = 0; i < n; i++) {
    m1 = m2;
    if (m1.find(l[i]) == m1.end())
      m1[l[i]] = c[i];
    else
      m1[l[i]] = min(m1[l[i]], c[i]);
    map<int, int>::iterator it;
    for (it = m2.begin(); it != m2.end(); ++it) {
      int g = gcd(it->first, l[i]);
      if (m1.find(g) == m1.end())
        m1[g] = it->second + c[i];
      else
        m1[g] = min(m1[g], it->second + c[i]);
    }
    m2 = m1;
    m1.clear();
  }
  if (m2.find(1) != m2.end())
    cout << m2[1];
  else
    cout << -1;
  return 0;
}
