#include <bits/stdc++.h>
using namespace std;
const int N = 100009;
const int NMAX = 100000;
const int oo = (int)(1e9);
int Prime[N];
bool NotPrime[N];
int n, a[309], c[309];
vector<int> save[309];
int dp[1 << 12], temp[1 << 12];
void Init() {
  for (int i = 1; i <= n; i++) {
    save[i].clear();
    int x = a[i];
    for (int j = 2; j * j <= a[i]; j++) {
      if (x % j == 0) save[i].push_back(j);
      while (x % j == 0) x /= j;
    }
    if (x != 1) save[i].push_back(x);
  }
}
void biton(unsigned int &p, int k) { p = p | (1 << k); }
int solve(int x) {
  unsigned int last = (1 << save[x].size()) - 1;
  int ans = oo;
  for (unsigned int s = 0; s <= last; s++) dp[s] = oo;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    unsigned int p = 0;
    for (int j = 0; j < save[x].size(); j++)
      if (a[i] % save[x][j]) biton(p, j);
    for (int s = 0; s <= last; s++) temp[s] = dp[s];
    for (int s = 0; s <= last; s++) dp[s | p] = min(dp[s | p], temp[s] + c[i]);
    ans = min(ans, dp[last]);
  }
  return ans + c[x];
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  Init();
  int ans = oo;
  for (int i = 1; i <= n; i++) {
    ans = min(ans, solve(i));
  }
  printf("%d", (ans == oo ? -1 : ans));
}
