#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  int a[305];
  long long b[305];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  map<int, long long int> std;
  for (int i = 0; i < n; i++) {
    if (std.count(a[i])) {
      std[a[i]] = min(std[a[i]], b[i]);
    } else
      std.insert(pair<int, long long int>(a[i], b[i]));
  }
  for (int i = 1; i < n; i++) {
    map<int, long long int>::iterator it;
    for (it = std.begin(); it != std.end(); it++) {
      int ans = gcd(it->first, a[i]);
      if (std.count(ans)) {
        std[ans] = min((long long)it->second + b[i], std[ans]);
      } else {
        std.insert(pair<int, long long int>(ans, (long long)b[i] + it->second));
      }
    }
  }
  if (std.count(1))
    cout << std[1] << endl;
  else
    cout << -1 << endl;
  return 0;
}
