#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
map<pair<long long, long long>, long long> p;
long long n;
const long long mxn = 305;
long long arr[mxn], brr[mxn];
long long solve(long long ind, long long g) {
  if (g == 1) {
    return 0;
  }
  if (ind >= n) {
    return 1e18;
  }
  if (p.count({ind, g})) {
    return p[{ind, g}];
  }
  long long res = 1e18;
  if (g == -1) {
    res = min(res, brr[ind] + solve(ind + 1, arr[ind]));
    res = min(res, solve(ind + 1, g));
  } else {
    res = min(res, brr[ind] + solve(ind + 1, gcd(g, arr[ind])));
    res = min(res, solve(ind + 1, g));
  }
  p[{ind, g}] = res;
  return res;
}
void snow() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> arr[i];
  for (long long i = 0; i < n; i++) cin >> brr[i];
  long long ans = solve(0, -1);
  if (ans >= 1e18) {
    ans = -1;
  }
  cout << ans << '\n';
}
int32_t main() {
  auto start = high_resolution_clock::now();
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long _;
  _ = 1;
  while (_--) snow();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);
  return 0;
}
