#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int l[310], c[310], n;
map<pair<int, int>, int> dp;
int fun(int i, int gd) {
  if (i == n) {
    if (gd == 1) return 0;
    return 1e9;
  }
  if (dp.find(make_pair(i, gd)) != dp.end()) return dp[make_pair(i, gd)];
  return dp[make_pair(i, gd)] =
             min(fun(i + 1, gd),
                 c[i] + fun(i + 1, gd == -1 ? l[i] : gcd(gd, l[i])));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  int ans = fun(0, -1);
  if (ans >= 1000000000)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
}
