#include <bits/stdc++.h>
using namespace std;
int l[100000], c[100000];
map<int, int> m;
int gcd(int a, int b) {
  if (b == 0) return (a);
  if (b > a) return (gcd(b, a));
  return (gcd(b, a % b));
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &l[i]);
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
  int g = -1;
  for (int i = 0; i < n; i++) {
    if (g == -1)
      g = l[i];
    else
      g = gcd(g, l[i]);
    if (m.find(l[i]) != m.end())
      m[l[i]] = min(m[l[i]], c[i]);
    else
      m[l[i]] = c[i];
  }
  if (g > 1)
    printf("-1\n");
  else {
    int sz = m.size();
    while (true) {
      n = 0;
      for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
        l[n] = it->first;
        c[n] = it->second;
        n++;
      }
      for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
          int tg = gcd(l[i], l[j]);
          int tc = c[i] + c[j];
          if (m.find(tg) != m.end())
            m[tg] = min(m[tg], tc);
          else
            m[tg] = tc;
        }
      }
      int nsz = m.size();
      if (nsz == sz)
        break;
      else
        sz = nsz;
    }
    printf("%d\n", m[1]);
  }
  return 0;
}
