#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
map<int, int>::iterator iter;
int l[305], c[305];
int gcd(int a, int b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
int main() {
  int n, temp, su;
  cin >> n;
  for (register int i = 1; i <= n; i++) cin >> l[i];
  for (register int i = 1; i <= n; i++) cin >> c[i];
  for (register int i = 1; i <= n; i++) {
    temp = m[l[i]];
    if (temp)
      m[l[i]] = min(temp, c[i]);
    else
      m[l[i]] = c[i];
    for (iter = m.begin(); iter != m.end(); iter++) {
      su = gcd(l[i], iter->first);
      temp = m[su];
      if (temp)
        m[su] = min(temp, iter->second + c[i]);
      else
        m[su] = iter->second + c[i];
    }
  }
  int ans = m[1];
  if (ans)
    cout << ans << endl;
  else
    cout << -1 << endl;
}
