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