#include <bits/stdc++.h>
using namespace std;
const int max_n = 300 + 10, inf = 1000 * 1000 * 900;
vector<int> p[max_n], b;
int n, a[max_n], c[max_n];
int dp[2 * max_n];
int solve(int v) {
  b.clear();
  for (int i = 0; i < (1 << (int)p[v].size()); i++) dp[i] = inf;
  for (int i = 0; i < n; i++)
    if (i != v) b.push_back(i);
  dp[(1 << (int)p[v].size()) - 1] = c[v];
  for (auto i : b) {
    int num = 0;
    for (auto j : p[v]) {
      num *= 2;
      bool check = false;
      for (auto k : p[i])
        if (j == k) check = true;
      if (check) num++;
    }
    for (int j = 0; j < (1 << (int)p[v].size()); j++)
      dp[j & num] = min(dp[j & num], dp[j] + c[i]);
  }
  return dp[0];
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    int hp = a[i];
    for (int j = 2; j * j <= hp; j++)
      if (hp % j == 0) {
        p[i].push_back(j);
        while (hp % j == 0) hp /= j;
      }
    if (hp > 1) p[i].push_back(hp);
  }
  int ans = inf;
  for (int i = 0; i < n; i++) ans = min(ans, solve(i));
  if (ans == inf)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
}
