#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int c;
  while (b) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
const int N = 500;
int n;
int len[N], cost[N];
const int INF = 1LL << 30;
unordered_map<long long, int> cache;
int brute(int k, int curGcd) {
  const long long P = int(1e9) + 7;
  long long hash = k * P + curGcd;
  auto iter = cache.find(hash);
  if (iter != cache.end()) {
    return iter->second;
  }
  if (curGcd == 1) {
    return 0;
  }
  if (k == n) {
    return INF;
  }
  int res = brute(k + 1, curGcd);
  int newGcd = gcd(curGcd, len[k]);
  if (newGcd != curGcd) {
    res = min(res, cost[k] + brute(k + 1, newGcd));
  }
  cache[hash] = res;
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &len[i]);
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &cost[i]);
  }
  int res = brute(0, 0);
  cout << (res == INF ? -1 : res) << "\n";
  return 0;
}
