#include <bits/stdc++.h>
using namespace std;
const int inf = (int)3e7 + 5;
int jumpp[305], cst[305];
vector<pair<int, int> > vec, vec2;
vector<int> divi;
int dp[305][1025];
int solve() {
  if (((int)(vec2.size())) == 0) return inf;
  for (int i = 0; i < 305; i++)
    for (int j = 0; j < 1025; j++) dp[i][j] = inf;
  for (int i = 0; i < ((int)(vec2.size())); i++)
    dp[i][vec2[i].second] = vec2[i].first;
  for (int i = 0; i < ((int)(vec2.size())); i++)
    for (int j = 0; j < 1025; j++) {
      dp[i + 1][j & vec2[i + 1].second] =
          min(dp[i + 1][j & vec2[i + 1].second], vec2[i + 1].first + dp[i][j]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  int ans = inf;
  for (int i = 0; i < ((int)(vec2.size())); i++) ans = min(ans, dp[i][0]);
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> jumpp[i];
  for (int i = 0; i < n; i++) cin >> cst[i];
  int ans = inf;
  vec.clear();
  for (int i = 0; i < n; i++) {
    if (jumpp[i] == 1)
      ans = min(ans, cst[i]);
    else {
      vec.push_back(make_pair(cst[i], jumpp[i]));
    }
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < ((int)(vec.size())); i++) {
    divi.clear();
    int jmp = vec[i].second;
    if (jmp % 2 == 0) {
      divi.push_back(2);
      while (jmp % 2 == 0) jmp /= 2;
    }
    for (int j = 3; j * j <= jmp; j += 2) {
      if (jmp % j == 0) {
        divi.push_back(j);
        while (jmp % j == 0) jmp /= j;
      }
    }
    if (jmp > 1) divi.push_back(jmp);
    vec2.clear();
    for (int j = 0; j < i; j++) {
      int mask = 0;
      for (int k = 0; k < ((int)(divi.size())); k++) {
        if (vec[j].second % divi[k] == 0) mask |= (1 << k);
      }
      vec2.push_back(make_pair(vec[j].first, mask));
    }
    ans = min(ans, vec[i].first + solve());
  }
  if (ans >= inf)
    cout << "-1\n";
  else
    cout << ans << '\n';
}
