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