#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 + 10;
int l[maxn], c[maxn];
map<int, int> dp;
int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &l[i]);
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (map<int, int>::iterator it = dp.begin(); it != dp.end(); ++it) {
      int t = gcd(it->first, l[i]);
      if (dp.count(t)) {
        dp[t] = min(dp[t], it->second + c[i]);
      } else
        dp[t] = it->second + c[i];
    }
  }
  if (!dp.count(1))
    printf("-1\n");
  else
    printf("%d\n", dp[1]);
  return 0;
}
