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