#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
int l[maxn], c[maxn];
int GCD(int a, int b) {
  if (!b) return a;
  return GCD(b, a % b);
}
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", &c[i]);
  map<int, int> f;
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto it : f) {
      int d = GCD(it.first, l[i]);
      if (f[d])
        f[d] = min(f[d], it.second + c[i]);
      else
        f[d] = it.second + c[i];
    }
  }
  if (f[1])
    printf("%d", f[1]);
  else
    printf("-1");
  return 0;
}
