#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:336777216")
using namespace std;
inline long long MAX(long long a, long long b) { return (a > b) ? (a) : (b); }
inline long long MIN(long long a, long long b) { return (a < b) ? (a) : (b); }
long long gcd(long long a, long long b) {
  long long r;
  while (b) {
    r = b;
    b = a % b;
    a = r;
  }
  return a;
}
int main() {
  int const sz = 2e3 + 10;
  long long n, m, a[sz + 1], cost[sz + 1], b, k, i, sum = 0, j, x, y, r, q, z,
                                                    dp[sz + 1], t;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) cin >> cost[i];
  map<long long, long long> mp;
  x = 1e18;
  for (i = 1; i <= n; i++)
    if (a[i] == 1) {
      x = MIN(x, cost[i]);
      mp[1] = x;
    }
  for (i = 2; i <= n; i++) {
    std::map<long long, long long>::iterator it;
    for (j = 1; j <= i - 1; j++) {
      x = gcd(a[j], a[i]);
      if (mp.find(x) != mp.end())
        mp[x] = MIN(cost[i] + cost[j], mp[x]);
      else
        mp[x] = cost[i] + cost[j];
    }
    for (it = mp.begin(); it != mp.end(); it++) {
      x = gcd(it->first, a[i]);
      if (mp.find(x) != mp.end())
        mp[x] = MIN(cost[i] + it->second, mp[x]);
      else
        mp[x] = cost[i] + it->second;
    }
  }
  if (mp.find(1) == mp.end())
    cout << "-1";
  else
    cout << mp[1];
  return 0;
}
