#include <bits/stdc++.h>
using namespace std;
int c[1000], n;
map<pair<int, long long>, long long> mp;
unsigned long long int l[1000];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int solve(int idx, long long cd) {
  if (cd == 1) return 0;
  if (idx == n) return 1000000000;
  if (mp[make_pair(idx, cd)] > 0) return mp[make_pair(idx, cd)];
  return mp[make_pair(idx, cd)] =
             min(solve(idx + 1, gcd(cd, l[idx])) + c[idx], solve(idx + 1, cd));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  int x = solve(0, 0);
  if (x == 1000000000)
    cout << -1 << endl;
  else
    cout << x << endl;
  return 0;
}
