#include <bits/stdc++.h>
using namespace std;
int dX[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dY[] = {1, -1, 0, 0, 1, -1, -1, 1};
long long GCD(long long num, long long rem) {
  return !rem ? num : GCD(rem, num % rem);
}
int n, len[350], cst[350];
map<pair<long long, long long>, long long> dp;
long long solve(int idx, int G) {
  if (G == 1) return 0;
  if (idx == n) return 4000000000000400000;
  if (dp.count({idx, G})) return dp[{idx, G}];
  return dp[{idx, G}] = min(solve(idx + 1, GCD(len[idx], G)) + cst[idx],
                            solve(idx + 1, G));
}
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", &cst[i]);
  long long ans = solve(0, 0);
  if (ans != 4000000000000400000)
    printf("%lld", ans);
  else
    puts("-1");
  return 0;
}
