#include <bits/stdc++.h>
using namespace std;
int n;
int l[305];
int c[305];
map<int, int> mp;
void ini() {
  int i;
  mp.clear();
  for (i = 1; i <= n; i++) {
    scanf("%d", &l[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &c[i]);
  }
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else {
    return gcd(b, a % b);
  }
}
void solve() {
  int i;
  int te;
  int cost;
  map<int, int>::iterator it;
  mp[0] = 0;
  for (i = 1; i <= n; i++) {
    for (it = mp.begin(); it != mp.end(); it++) {
      te = gcd(l[i], it->first);
      cost = c[i] + it->second;
      if (mp[te] == 0) {
        mp[te] = cost;
      } else {
        mp[te] = min(mp[te], cost);
      }
    }
  }
}
void out() {
  if (mp[1] == 0) {
    printf("-1\n");
  } else {
    printf("%d\n", mp[1]);
  }
}
int main() {
  while (scanf("%d", &n) != EOF) {
    ini();
    solve();
    out();
  }
  return 0;
}
