#include <bits/stdc++.h>
using namespace std;
int a[310], n, cst[310];
int dp[310][1 << 10];
int cc[1 << 10];
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int func(int here, int ch) {
  if (here == n) {
    if (ch == 0)
      return 0;
    else
      return 1234567890;
  }
  int &ret = dp[here][ch];
  if (ret != -1) return ret;
  ret = 1234567890;
  ret = min(ret, func(here + 1, ch));
  ret = min(ret, func(here + 1, (ch & cc[here])) + cst[here]);
  return ret;
}
int p[33000];
vector<int> vt;
int main() {
  for (int i = 2; i < 33000; i++) {
    if (p[i]) continue;
    vt.push_back(i);
    for (int y = i + i; y < 33000; y += i) {
      p[y] = 1;
    }
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%d", &cst[i]);
  int ch = a[0];
  for (int i = 1; i < n; i++) ch = gcd(ch, a[i]);
  if (ch != 1) {
    puts("-1");
    return 0;
  }
  int dap = 1234567890;
  for (int i = 0; i < n; i++) {
    vector<int> vt1;
    for (int y = 0; y < vt.size(); y++) {
      if (a[i] % vt[y] == 0) {
        vt1.push_back(vt[y]);
        while (a[i] % vt[y] == 0) {
          a[i] /= vt[y];
        }
      }
    }
    if (a[i] != 1) vt1.push_back(a[i]);
    for (int y = i + 1; y < n; y++) {
      int xx = 0;
      for (int z = 0; z < vt1.size(); z++)
        if (a[y] % vt1[z] == 0) xx += (1 << z);
      cc[y] = xx;
    }
    memset(dp, -1, sizeof(dp));
    dap = min(dap, func(i + 1, (1 << vt1.size()) - 1) + cst[i]);
  }
  printf("%d\n", dap);
}
