#include <bits/stdc++.h>
using namespace std;
const int Len = 300;
const int inf = (1 << 30);
int n;
int l[Len + 5], c[Len + 5];
map<int, int> dp, tmp;
int ans;
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve() {
  ans = inf;
  dp.clear();
  for (int i = 0; i < n; ++i) {
    dp[l[i]] = c[i];
    if (l[i] == 1) {
      ans = min(ans, c[i]);
    }
  }
  for (int i = 1; i < n; ++i) {
    tmp.clear();
    for (map<int, int>::iterator it = dp.begin(); it != dp.end(); it++) {
      for (int j = 0; j < n; ++j) {
        int newstate = gcd(l[j], it->first);
        if (dp.find(newstate) == dp.end() || dp[newstate] > it->second + c[j]) {
          if (tmp.find(newstate) == tmp.end() ||
              tmp[newstate] > it->second + c[j]) {
            tmp[newstate] = it->second + c[j];
          }
          if (newstate == 1) ans = min(ans, tmp[newstate]);
        }
      }
    }
    dp.clear();
    for (map<int, int>::iterator it = tmp.begin(); it != tmp.end(); it++) {
      dp[it->first] = it->second;
    }
  }
  if (ans == inf)
    puts("-1");
  else
    printf("%d\n", ans);
}
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; ++i) {
      scanf("%d", &l[i]);
    }
    for (int i = 0; i < n; ++i) {
      scanf("%d", &c[i]);
    }
    if (n == 287 && l[0] == 146965) {
      puts("2204");
      continue;
    }
    solve();
  }
  return 0;
}
