#include <bits/stdc++.h>
using namespace std;
map<pair<long long int, long long int>, long long int> dp;
const int N = 303;
int n;
long long int l[N], c[N];
long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long int solve(int indx, long long int jump) {
  if (indx == n || jump == 1) {
    if (jump == 1) return 0;
    return 1000000000;
  }
  if (dp.count({indx, jump}) > 0) return dp[{indx, jump}];
  return dp[{indx, jump}] = min(solve(indx + 1, gcd(jump, l[indx])) + c[indx],
                                solve(indx + 1, jump));
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%I64d", l + i);
  for (int i = 0; i < n; i++) scanf("%I64d", c + i);
  long long int sol = solve(0, 0);
  cout << (sol == 1000000000 ? -1 : sol);
  return 0;
}
