#include <bits/stdc++.h>
using namespace std;
int n;
int l[100100];
int c[100100];
map<long long, long long> dp[330][2];
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
long long f(int i, bool b, long long g) {
  if (i == n) {
    if (g == 1) return 0;
    return 1e8;
  }
  if (dp[i][b].count(g)) return dp[i][b][g];
  long long r = f(i + 1, 1, gcd(l[i], g)) + c[i];
  r = min(r, f(i + 1, b, g));
  return dp[i][b][g] = r;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  long long r = f(0, 0, 0);
  if (r >= 1e8) r = -1;
  cout << r << endl;
  return 0;
}
