#include <bits/stdc++.h>
using namespace std;
int n, now, top, ans;
int l[310], c[310], q[10000000];
map<int, int> f;
int gcd(int a, int b) {
  if (a < b) return gcd(b, a);
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  scanf("%d", &n);
  ans = 99999999;
  for (int i = 1; i <= n; ++i) scanf("%d", &l[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
  f[0] = 0;
  for (int i = 1; i <= n; ++i) {
    map<int, int>::iterator it = f.begin();
    for (; it != f.end(); ++it) {
      now = gcd(l[i], it->first);
      q[++top] = now;
      if (f[now] != 0) {
        f[now] = min(f[now], it->second + c[i]);
      } else {
        f[now] = it->second + c[i];
      }
    }
  }
  if (f[1] != 0) {
    printf("%d", f[1]);
  } else
    printf("-1");
  return 0;
}
