#include <bits/stdc++.h>
using namespace std;
map<int, int> Dn;
int L[310];
int C[310];
int N;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", L + i);
  for (int i = 1; i <= N; i++) scanf("%d", C + i);
  for (int i = 1; i <= N; i++) {
    if (Dn.find(L[i]) == Dn.end() || Dn[L[i]] > C[i]) Dn[L[i]] = C[i];
    for (map<int, int>::iterator it = Dn.begin(); it != Dn.end(); it++) {
      int temp = gcd(it->first, L[i]);
      if (Dn.find(temp) == Dn.end() || Dn[temp] > it->second + C[i]) {
        Dn[temp] = it->second + C[i];
      }
    }
  }
  if (Dn.find(1) == Dn.end())
    printf("-1\n");
  else
    printf("%d\n", Dn[1]);
  return 0;
}
