#include <bits/stdc++.h>
using namespace std;
map<int, int> m;
inline int read() {
  int a = 0, q = 0;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') q = 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    a = (a << 3) + (a << 1) + ch - 48, ch = getchar();
  return q ? -a : a;
}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int N = 1000;
int num[N], mon[N], n, temp;
int main() {
  while (~scanf("%d", &n)) {
    for (register int i = 1; i <= n; ++i) num[i] = read();
    for (register int i = 1; i <= n; ++i) mon[i] = read();
    m.clear();
    map<int, int>::iterator it;
    m[0] = 0;
    for (register int i = 1; i <= n; ++i) {
      for (it = m.begin(); it != m.end(); ++it) {
        temp = gcd(num[i], it->first);
        int temp1 = it->second + mon[i];
        if (m[temp] && m[temp] < temp1) continue;
        m[temp] = temp1;
      }
    }
    if (!m[1])
      printf("-1\n");
    else
      printf("%d\n", m[1]);
  }
  return 0;
}
