#include <bits/stdc++.h>
using namespace std;
long long dp[10101010];
long long a[101010], b[101010];
vector<int> d;
set<long long> s;
int m;
void f(int x) {
  for (long long i = 1; i * i <= x; ++i)
    if (x % i == 0) {
      s.insert(i);
      s.insert(x / i);
    }
  s.insert(x);
}
int bp(long long x) {
  int l = 0, r = m - 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (d[mid] < x)
      l = mid;
    else
      r = mid;
  }
  if (d[l] == x)
    return l;
  else
    return r;
}
int f_gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  while (a && b) {
    a %= b;
    swap(a, b);
  }
  return a + b;
}
int main() {
  long long n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    f(a[i]);
  }
  for (set<long long>::iterator it = s.begin(); it != s.end(); ++it)
    d.push_back(*it);
  m = d.size();
  for (int i = 0; i < n; ++i) cin >> b[i];
  for (int i = 0; i < m; ++i) dp[i] = 2e18;
  for (int i = 0; i < n; ++i) {
    int pos = bp(a[i]);
    dp[pos] = min(dp[pos], b[i]);
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      int gcd = f_gcd(a[i], d[j]);
      int pos = bp(gcd);
      dp[pos] = min(dp[pos], dp[j] + b[i]);
    }
  if (dp[0] == 2e18)
    cout << -1;
  else
    cout << dp[0];
  return 0;
}
