#include <bits/stdc++.h>
using namespace std;
int n;
int l[305];
int c[305];
map<int, int> dp[305];
int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = int(0); i <= int(n - 1); ++i) {
    cin >> l[i];
  }
  for (int i = int(0); i <= int(n - 1); ++i) {
    cin >> c[i];
  }
  dp[0][0] = 0;
  for (int i = int(0); i <= int(n - 1); ++i) {
    dp[i + 1] = dp[i];
    for (typeof(dp[i].begin()) it = dp[i].begin(); it != dp[i].end(); ++it) {
      int g = it->first, temp = it->second;
      if (dp[i + 1].find(gcd(g, l[i])) == dp[i + 1].end()) {
        dp[i + 1][gcd(g, l[i])] = temp + c[i];
      } else {
        dp[i + 1][gcd(g, l[i])] = min(dp[i + 1][gcd(g, l[i])], temp + c[i]);
      }
    }
  }
  if (dp[n].find(1) != dp[n].end()) {
    cout << dp[n][1] << endl;
  } else
    cout << -1 << endl;
  return 0;
}
