#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int inf = 0x3c3c3c3c;
const long long infl = 0x3c3c3c3c3c3c3c3c;
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int N;
int l[303], c[303];
map<pair<int, int>, int> mp;
int go(int pos, int g) {
  if (pos == N) return g == 1 ? 0 : inf;
  auto cache = mp.find({pos, g});
  if (cache != mp.end()) return (*cache).second;
  int ret = inf;
  ret = min(ret, c[pos] + go(pos + 1, g == -1 ? l[pos] : gcd(g, l[pos])));
  ret = min(ret, go(pos + 1, g));
  return mp[{pos, g}] = ret;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> l[i];
  for (int i = 0; i < N; i++) cin >> c[i];
  int ret = go(0, -1);
  if (ret == inf)
    cout << -1;
  else
    cout << ret;
  return 0;
}
