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