#include <bits/stdc++.h>
using namespace std;
inline void rd(int &x) {
  x = 0;
  bool flag = 0;
  char c = getchar();
  while ((c < '0' || c > '9') && c != '-') c = getchar();
  if (c == '-') flag = 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + (c - '0'), c = getchar();
  if (flag) x = -x;
  return;
}
int n;
struct node {
  int l, c;
  inline bool operator<(const node &X) const { return l < X.l; }
} x[10000];
map<int, int> mp;
map<int, int>::iterator it;
inline int gcd(int x, int y) { return x ? gcd(y % x, x) : y; }
int main() {
  rd(n);
  for (int i = 1; i <= n; ++i) {
    rd(x[i].l);
  }
  for (int i = 1; i <= n; ++i) {
    rd(x[i].c);
  }
  for (int i = 1; i <= n; ++i) {
    int z = mp[x[i].l];
    if (z)
      mp[x[i].l] = min(mp[x[i].l], x[i].c);
    else
      mp[x[i].l] = x[i].c;
    for (it = mp.begin(); it != mp.end(); it++) {
      int k = gcd(x[i].l, it->first);
      z = mp[k];
      if (z)
        mp[k] = min(z, it->second + x[i].c);
      else
        mp[k] = it->second + x[i].c;
    }
  }
  int ans = mp[1];
  if (ans)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
