#include <bits/stdc++.h>
using namespace std;
const int MAXN = 310;
map<int, int> Map;
int l[MAXN], c[MAXN], n;
int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &l[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
  for (int i = 1; i <= n; ++i) {
    if (Map[l[i]])
      Map[l[i]] = min(Map[l[i]], c[i]);
    else
      Map[l[i]] = c[i];
    for (map<int, int>::iterator it = Map.begin(); it != Map.end(); it++) {
      int x = gcd(l[i], it->first);
      if (Map[x])
        Map[x] = min(Map[x], c[i] + it->second);
      else
        Map[x] = c[i] + it->second;
    }
  }
  if (Map[1])
    printf("%d", Map[1]);
  else
    printf("-1");
  return 0;
}
