#include <bits/stdc++.h>
using namespace std;
int n;
int c[310], l[310];
inline int gcd(int a, int b) { return (!b) ? a : gcd(b, a % b); }
map<int, int> Mp[2];
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]);
  }
  Mp[0][l[1]] = c[1];
  Mp[0][0] = 0;
  for (int i = 2; i <= n; i++) {
    Mp[(i & 1) ^ 1].clear();
    for (map<int, int>::iterator j = Mp[i & 1].begin(); j != Mp[i & 1].end();
         j++) {
      int now = gcd(l[i], (*j).first);
      if (Mp[(i & 1) ^ 1].find(now) != Mp[(i & 1) ^ 1].end()) {
        Mp[(i & 1) ^ 1][now] = min(Mp[(i & 1) ^ 1][now], (*j).second + c[i]);
      } else
        Mp[(i & 1) ^ 1][now] = (*j).second + c[i];
      now = (*j).first;
      if (Mp[(i & 1) ^ 1].find(now) != Mp[(i & 1) ^ 1].end()) {
        Mp[(i & 1) ^ 1][now] = min(Mp[(i & 1) ^ 1][now], (*j).second);
      } else
        Mp[(i & 1) ^ 1][now] = (*j).second;
    }
  }
  if (Mp[(n & 1) ^ 1].find(1) != Mp[(n & 1) ^ 1].end())
    printf("%d\n", Mp[(n & 1) ^ 1][1]);
  else
    printf("-1\n");
}
