#include <bits/stdc++.h>
using namespace std;
const int maxn = 303;
int val[maxn], cost[maxn];
map<int, int> mp;
queue<int> P;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &cost[i]);
  for (int i = 1; i <= n; i++) {
    if (mp.find(val[i]) != mp.end()) {
      mp[val[i]] = min(mp[val[i]], cost[i]);
      continue;
    }
    mp[val[i]] = cost[i];
    P.push(val[i]);
  }
  while (!P.empty()) {
    int v = P.front();
    P.pop();
    for (int i = 1; i <= n; i++) {
      int t = gcd(v, val[i]);
      if (mp.find(t) == mp.end() || mp[v] + cost[i] < mp[t]) {
        mp[t] = mp[v] + cost[i];
        P.push(t);
      }
    }
  }
  if (mp.find(1) == mp.end())
    printf("-1\n");
  else
    printf("%d\n", mp[1]);
  return 0;
}
