#include <bits/stdc++.h>
using namespace std;
int n;
int l[333];
int c[333];
int ag[5000000], k = 0;
int ac[5000000];
map<int, int> map_gcd;
map<int, int>::iterator ii;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    k = 0;
    for (ii = map_gcd.begin(); ii != map_gcd.end(); ii++) {
      ag[k] = gcd(ii->first, l[i]);
      ac[k] = (ii->second + c[i]);
      k++;
    }
    ag[k] = l[i];
    ac[k] = (c[i]);
    k++;
    for (int i = 0; i < k; i++) {
      if (!map_gcd.count(ag[i])) {
        map_gcd[ag[i]] = ac[i];
      } else if (map_gcd[ag[i]] > ac[i]) {
        map_gcd[ag[i]] = ac[i];
      }
    }
  }
  if (map_gcd.count(1)) {
    cout << map_gcd[1] << endl;
  } else {
    cout << "-1" << endl;
  }
}
