#include <bits/stdc++.h>
using namespace std;
const int N = 303, M = 1 << 10, INF = 1e9 + 7;
int n;
int a[N];
int cost[N];
int mem[N][M];
int color[N][M];
int col = 0;
int mask[N];
int dp(int i, int msk) {
  if (i == n) {
    return msk == 0 ? 0 : INF;
  }
  int &ret = mem[i][msk];
  if (color[i][msk] == col) return ret;
  color[i][msk] = col;
  ret = INF;
  ret = dp(i + 1, msk);
  ret = min(ret, dp(i + 1, msk & mask[i]) + cost[i]);
  if (ret >= INF) ret = INF;
  return ret;
}
int work(int x, int dx) {
  vector<int> primes;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      primes.push_back(i);
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) primes.push_back(x);
  memset(mask, 0, sizeof mask);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < primes.size(); j++)
      if (a[i] % primes[j] == 0) mask[i] |= (1 << j);
  }
  col++;
  return dp(0, (1 << (int)primes.size()) - 1) + cost[dx];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &cost[i]);
  }
  int ans = INF;
  for (int i = 0; i < n; i++) {
    ans = min(ans, work(a[i], i));
  }
  printf("%d\n", ans == INF ? -1 : ans);
  return 0;
}
