#include <bits/stdc++.h>
using namespace std;
int l[500005], v[500005];
map<int, int> mp;
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", l + i);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", v + i);
  }
  mp[0] = 0;
  map<int, int>::iterator j;
  for (int i = 1; i <= n; i++) {
    for (j = mp.begin(); j != mp.end(); j++) {
      int t = gcd(j->first, l[i]);
      if (mp.count(t)) {
        mp[t] = min(mp[t], j->second + v[i]);
      } else {
        mp[t] = j->second + v[i];
      }
    }
  }
  if (!mp.count(1))
    printf("-1\n");
  else
    printf("%d\n", mp[1]);
  return 0;
}
