#include <bits/stdc++.h>
using namespace std;
map<long long, long long> dp;
long long N;
long long L[300];
long long C[300];
const long long INF = 10000000000000000;
inline long long gcd(long long a, long long b) {
  if (a == INF) return b;
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
inline long long def(long long x) { return x == 0 ? INF : x; }
int main() {
  scanf("%lld", &N);
  for (long long i = 0; i < N; i++) scanf("%lld", &L[i]);
  for (long long i = 0; i < N; i++) scanf("%lld", &C[i]);
  for (long long i = 0; i < N; i++) {
    long long adding = L[i];
    long long cost = C[i];
    dp[adding] = min(def(dp[adding]), cost);
    for (auto& p : dp) {
      long long current = p.first;
      long long currentcost = p.second;
      long long nextnum = gcd(current, adding);
      dp[nextnum] = min(def(dp[nextnum]), currentcost + cost);
    }
    for (auto& p : dp) {
      ("(%lld,%lld)", p.first, p.second);
    }
    ("\n");
  }
  printf("%lld\n", dp[1] == 0 ? -1 : dp[1]);
  return 0;
}
