#include <bits/stdc++.h>
using namespace std;
long long ans = 100000000007;
vector<long long> v;
int n;
long long dp[302][1025];
long long arr[301], cost[301];
void foo(int ind, int x) {
  if (ind > n) return;
  unsigned int num = 0;
  for (int i = 0; i < v.size(); i++) {
    if (arr[ind] % v[i] == 0)
      num = num << 1 | 1;
    else
      num = num << 1;
  }
  int m = 1 << v.size();
  for (int i = 0; i < m; i++)
    dp[ind + 1][i] =
        min(dp[ind][i], dp[ind][(i | (num ^ (m - 1)))] + cost[ind]);
  foo(ind + 1, x);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) cin >> cost[i];
  for (int i = 0; i < n; i++) {
    v.clear();
    int y = arr[i];
    for (int j = 2; j * j <= arr[i]; j++) {
      if (y % j == 0) v.push_back(j);
      while (y % j == 0) y /= j;
    }
    if (y > 1) v.push_back(y);
    for (int j = 0; j < (1 << v.size()); j++) dp[0][j] = 100000000007;
    dp[0][(1 << v.size()) - 1] = cost[i];
    foo(0, i);
    ans = min(ans, dp[n][0]);
  }
  if (ans < 100000000007)
    cout << ans << endl;
  else
    cout << -1 << endl;
  return 0;
}
