#include <bits/stdc++.h>
using namespace std;
int n;
int l[310], c[310];
map<int, int> mp;
priority_queue<int> q;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) scanf("%d", l + i);
  for (int i = 0; i < n; ++i) scanf("%d", c + i);
  for (int i = 0; i < n; ++i) {
    if (mp.count(l[i]))
      mp[l[i]] = min(mp[l[i]], c[i]);
    else {
      mp[l[i]] = c[i];
      q.push(l[i]);
    }
  }
  while (!q.empty()) {
    int k = q.top();
    q.pop();
    for (int i = 0; i < n; ++i) {
      int t = gcd(k, l[i]);
      int cost = mp[k] + mp[l[i]];
      if (mp.find(t) == mp.end() || mp[t] > cost) {
        mp[t] = cost;
        q.push(t);
      }
    }
  }
  if (!mp[1])
    printf("-1\n");
  else
    printf("%d\n", mp[1]);
  return 0;
}
