#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000;
const long long INF64 = 1LL * INF * INF;
const long long mod = INF + 7;
int l[310], c[310];
int n;
map<int, int> dp;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void solve() {
  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 it : dp) {
      int m_gcd = gcd(it.first, l[i]);
      if (!dp.count(m_gcd)) dp[m_gcd] = INF;
      dp[m_gcd] = min(dp[m_gcd], it.second + c[i]);
    }
  }
  if (!dp.count(1))
    cout << -1 << endl;
  else
    cout << dp[1] << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
