#include <bits/stdc++.h>
using namespace std;
const int N = 303;
map<int, int> dp[N];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> l(n), c(n);
  for (auto &i : l) {
    cin >> i;
  }
  for (auto &i : c) {
    cin >> i;
  }
  for (int i = 0; i < n; ++i) {
    dp[i + 1][l[i]] = c[i];
    for (auto it = dp[i].begin(); it != dp[i].end(); ++it) {
      int to = gcd(it->first, l[i]);
      if (dp[i + 1].count(to)) {
        dp[i + 1][to] = min(dp[i + 1][to], it->second + c[i]);
      } else {
        dp[i + 1][to] = it->second + c[i];
      }
      to = it->first;
      if (dp[i + 1].count(to)) {
        dp[i + 1][to] = min(dp[i + 1][to], it->second);
      } else {
        dp[i + 1][to] = it->second;
      }
    }
  }
  if (dp[n].count(1))
    cout << dp[n][1] << '\n';
  else
    cout << "-1\n";
}
