#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int n;
pair<int, int> a[N];
map<int, int> f;
map<int, int>::iterator it;
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", &a[i].first);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i].second);
  f.clear();
  f[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int x = a[i].first, c = a[i].second;
    for (it = f.begin(); it != f.end(); ++it) {
      int y = it->first;
      int d = gcd(x, y);
      int t = (it->second) + c;
      if (f[d] && f[d] < t) continue;
      f[d] = t;
    }
  }
  printf("%d\n", !f[1] ? -1 : f[1]);
  return 0;
}
