#include <bits/stdc++.h>
long long int ciel(double a) {
  long long int ret = a;
  if (a - ret > 0.0000000001) ret++;
  return ret;
}
long long int gcd(long long int a, long long int b) {
  if (a < b) return gcd(b, a);
  if (a % b == 0) return b;
  return gcd(b, a % b);
}
double fgcd(double a, double b) {
  if (fabs(a) < fabs(b)) return fgcd(b, a);
  if (fabs(b) < 0.0001) return a;
  return fgcd(b, fmod(a, b));
}
bool db_db_cmp(double a, double b) {
  return (a + 0.0000000001 > b && a - 0.0000000001 < b);
}
long long int fact[100005];
void generate_factorials() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i <= 100005 - 1; i++)
    fact[i] = ((fact[i - 1] * i) % 1000000007);
}
long long int pow(long long int n, long long int p) {
  if (p == 0) return 1;
  if (n <= 1) return n;
  long long int res = 1;
  while (p) {
    if (p & 1) res = res * n;
    n = n * n;
    p /= 2;
  }
  return res;
}
long long int mod_pow(long long int n, long long int p) {
  if (p == 0) return 1;
  if (n <= 1) return n;
  long long int res = 1;
  while (p) {
    if (p & 1) res = ((res * n) % 1000000007);
    n = ((n * n) % 1000000007);
    p /= 2;
  }
  return res;
}
long long int get_inverse(long long int n) {
  return mod_pow(n, 1000000007 - 2);
}
long long int get_nCr(long long int n, long long int r) {
  return ((fact[n] * get_inverse(((fact[r] * fact[n - r]) % 1000000007))) %
          1000000007);
}
using namespace std;
int sieve[100005];
vector<int> primes;
void calc_sieve() {
  for (int i = 0; i < 100005; i++) sieve[i] = 0;
  sieve[0] = sieve[1] = -1;
  for (int i = 2; i < 100005; i++) {
    if (sieve[i] == 0) {
      primes.push_back(i);
      for (int j = i; j < 100005; j = j + i) {
        sieve[j]++;
      }
    }
  }
}
vector<int> nums, cost;
int dp[1 << 11];
void preprocess() { calc_sieve(); }
void init() {}
vector<int> cur_primes;
void solve(int test_case) {
  init();
  int n;
  cin >> n;
  int g = -1;
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    nums.push_back(a);
    if (g == -1)
      g = a;
    else
      g = gcd(g, a);
  }
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    cost.push_back(a);
  }
  if (g > 1) {
    cout << "-1\n";
    return;
  }
  for (int i = 0; i <= (int)nums.size() - 1; i++) {
    cur_primes.clear();
    if (nums[i] == 1) {
      if (ans == -1 || ans > cost[i]) ans = cost[i];
      continue;
    }
    int temp = nums[i];
    for (int j = 0; j < primes.size() && primes[j] * primes[j] <= temp; j++) {
      if (temp % primes[j] == 0) {
        cur_primes.push_back(primes[j]);
        while (temp % primes[j] == 0) temp /= primes[j];
      }
    }
    if (temp > 1) cur_primes.push_back(temp);
    for (int j = 0; j <= (1 << cur_primes.size()) - 1; j++) {
      dp[j] = -1;
    }
    for (int j = i + 1; j <= (int)nums.size() - 1; j++) {
      int cur = 0;
      for (int k = 0; k <= (int)cur_primes.size() - 1; k++) {
        if (nums[j] % cur_primes[k] == 0) cur |= (1 << k);
      }
      if (dp[cur] == -1 || dp[cur] > cost[j]) dp[cur] = cost[j];
    }
    for (int j = (1 << cur_primes.size()) - 1; j >= 1; j--) {
      for (int k = j + 1; k <= (1 << cur_primes.size()) - 1; k++) {
        int temp = j & k;
        if (dp[j] != -1 && dp[k] != -1 &&
            (dp[temp] == -1 || dp[temp] > dp[j] + dp[k]))
          dp[temp] = dp[j] + dp[k];
      }
    }
    if (dp[0] != -1 && (ans == -1 || ans > dp[0] + cost[i]))
      ans = dp[0] + cost[i];
  }
  cout << ans << endl;
}
int main() {
  preprocess();
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve(i);
  }
  return 0;
}
