#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n, l[300], c[300];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> l[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  unordered_map<int, int> um;
  for (int i = 0; i < n; i++)
    if (um.find(l[i]) == um.end())
      um.emplace(l[i], c[i]);
    else
      um[l[i]] = min(um[l[i]], c[i]);
  for (int i = 0; i < n; i++) {
    for (auto elem : um) {
      int g = gcd(elem.first, l[i]);
      if (um.find(g) != um.end())
        um[g] = min(um[g], elem.second + c[i]);
      else
        um.emplace(g, elem.second + c[i]);
    }
  }
  cout << (um[1] ?: -1) << endl;
  return 0;
}
