#include <bits/stdc++.h>
using namespace std;
const int maxn = 305;
int n, l[maxn], c[maxn];
map<int, int> use;
struct node {
  int l, c;
  node() {}
  node(int a, int b) : l(a), c(b) {}
  inline bool operator<(const node &b) const { return c > b.c; }
};
priority_queue<node> yr;
int gcd(int a, int b) {
  if (b) return gcd(b, a % b);
  return a;
}
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]);
    use[l[i]] = c[i];
    yr.push(node(l[i], c[i]));
  }
  while (!yr.empty()) {
    node tmp = yr.top();
    yr.pop();
    if (tmp.l == 1) {
      printf("%d\n", tmp.c);
      return 0;
    }
    if (use[tmp.l] < tmp.c) continue;
    for (int i = 1; i <= n; i++) {
      int g = gcd(tmp.l, l[i]);
      if (use.find(g) != use.end() && use[g] <= tmp.c + c[i]) continue;
      if (g < tmp.l && g < l[i]) {
        use[g] = tmp.c + c[i];
        yr.push(node(g, tmp.c + c[i]));
      }
    }
  }
  printf("-1\n");
}
