#include <bits/stdc++.h>
using namespace std;
int arr[405], n, q, c[400];
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
void Readinput() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
}
void solve() {
  int i, j;
  map<long long, long long> ans, x, y, temp;
  map<long long, long long>::iterator iter;
  for (i = 0; i < n; i++) {
    for (iter = ans.begin(); iter != ans.end(); iter++) {
      long long num = iter->first;
      int k = gcd(arr[i], num);
      if (ans[k] == 0)
        ans[k] = ans[num] + c[i];
      else
        ans[k] = ((ans[k]) < (ans[num] + c[i]) ? ans[k] : ans[num] + c[i]);
    }
    if (ans[arr[i]] == 0)
      ans[arr[i]] = c[i];
    else
      ans[arr[i]] = ((ans[arr[i]]) < (c[i]) ? ans[arr[i]] : c[i]);
  }
  if (ans[1] == 0)
    printf("-1\n");
  else
    printf("%I64d\n", ans[1]);
}
int main() {
  Readinput();
  solve();
  return 0;
}
