#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
const int N = 3e2 + 19;
int n;
pair<int, int> a[N];
map<int, int> mp;
int gcd(int x, int y);
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i].first;
  for (int i = 1; i <= n; i++) cin >> a[i].second;
  mp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto j : mp) {
      int tmp = gcd(j.first, a[i].first);
      if (!mp[tmp] || mp[tmp] > mp[j.first] + a[i].second)
        mp[tmp] = mp[j.first] + a[i].second;
    }
  }
  if (mp[1] == 0)
    cout << -1;
  else
    cout << mp[1];
  return 0;
}
int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
