#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, long long> m;
long long INF = 1e9;
long long a[400], b[400];
long long n;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long f(long long i, long long g) {
  if (i == n) {
    if (g == 1) {
      return 0;
    }
    return INF;
  }
  if (m.find(make_pair(i, g)) != m.end()) {
    return m[make_pair(i, g)];
  }
  long long ans = f(i + 1, g);
  long long ng;
  if (g == 0) {
    ng = a[i];
  } else {
    ng = gcd(g, a[i]);
  }
  ans = min(ans, b[i] + f(i + 1, ng));
  return m[make_pair(i, g)] = ans;
}
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n; i++) {
    cin >> b[i];
  }
  long long ans = f(0, 0);
  if (ans >= INF) {
    ans = -1;
  }
  cout << ans << "\n";
  return 0;
}
