#include <bits/stdc++.h>
using namespace std;
map<int, int> dp;
int l[305], c[305];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (auto x : dp) {
      int a = gcd(x.first, l[i]);
      if (dp.count(a))
        dp[a] = min(dp[a], x.second + c[i]);
      else
        dp[a] = x.second + c[i];
    }
  }
  if (dp.count(1))
    cout << dp[1] << endl;
  else
    cout << -1 << endl;
  return 0;
}
