#include <bits/stdc++.h>
using namespace std;
int l[305], c[305];
unordered_map<int, int> f;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int main() {
  int n, g = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &l[i]), g = gcd(l[i], g);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  if (g != 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (auto it = f.begin(); it != f.end(); it++) {
      int t = gcd(l[i], it->first);
      if ((f.find(t) == f.end()) || f[t] > it->second + c[i])
        f[t] = it->second + c[i];
    }
    if ((f.find(l[i]) == f.end()) || f[l[i]] > c[i]) f[l[i]] = c[i];
  }
  printf("%d", f[1]);
  return 0;
}
