#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int cost[310];
int nata[310];
int n;
map<pair<long long, long long>, long long> mp;
long long tgrba(long long idx, long long last) {
  if (idx == n) {
    if (last == 1)
      return 0;
    else
      return 100000000000000000;
  }
  if (mp.count({idx, last}) > 0) return mp[{idx, last}];
  return mp[{idx, last}] =
             min(tgrba(idx + 1, last),
                 (tgrba(idx + 1, gcd(nata[idx], last)) + cost[idx]));
}
int main() {
  int i;
  scanf("%d", &n);
  ;
  for (i = 0; i < n; i++) {
    scanf("%d", &nata[i]);
    ;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &cost[i]);
    ;
  }
  long long ans = tgrba(0, 0);
  if (ans >= 100000000000000000)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
