#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a > b) return gcd(b, a);
  if (a == 0) return b;
  return gcd(b % a, a);
}
map<int, int> m;
int n;
int a[310], c[310];
int main() {
  scanf("%d", &n);
  m[0] = 0;
  for (int i = 1; i <= (n); ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= (n); ++i) {
    scanf("%d", &c[i]);
  }
  for (int i = 1; i <= (n); ++i) {
    for (__typeof((m).begin()) it = ((m).begin()); it != (m).end(); ++it) {
      int b = gcd(a[i], it->first);
      if (!((m).find(b) != (m).end()))
        m[b] = c[i] + it->second;
      else
        m[b] = ((m[b]) < (c[i] + it->second) ? (m[b]) : (c[i] + it->second));
    }
  }
  if (!((m).find(1) != (m).end()))
    printf("-1\n");
  else
    printf("%d\n", m[1]);
  return 0;
}
