#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e4 + 9;
bool mark[maxn];
int pn;
int pri[maxn];
void ini() {
  int a, b, c, d, e, f, g, h;
  for (a = 2; a <= 4e4; a++) {
    if (mark[a]) continue;
    pri[pn++] = a;
    for (d = a; d <= 4e4; d += a) {
      mark[d] = 1;
    }
  }
}
int arr[1111];
int cost[1111];
int yin[111];
int num;
void sol(int a) {
  num = 0;
  for (int i = 0; i < pn && a > 1; i++) {
    if (a % pri[i] == 0) {
      yin[num++] = pri[i];
    }
    while (a % pri[i] == 0) {
      a /= pri[i];
    }
  }
  if (a > 1) yin[num++] = a;
}
int dp[11111];
int main() {
  int a, b, c, d, e, f, g, h;
  ini();
  scanf("%d", &a);
  for (int d = 1; d <= a; d++) {
    scanf("%d", &arr[d]);
  }
  for (int d = 1; d <= a; d++) {
    scanf("%d", &cost[d]);
  }
  g = 1e9;
  for (d = 1; d <= a; d++) {
    sol(arr[d]);
    for (e = 0; e < (1 << num); e++) {
      dp[e] = 1e9;
    }
    dp[(1 << num) - 1] = cost[d];
    for (e = d + 1; e <= a; e++) {
      h = 0;
      for (int i = 0; i < num; i++) {
        if (arr[e] % yin[i] == 0) {
          h += (1 << i);
        }
      }
      for (int i = (1 << num) - 1; i > -1; i--) {
        dp[i & h] = min(dp[i & h], dp[i] + cost[e]);
      }
    }
    g = min(g, dp[0]);
  }
  if (g == 1e9) {
    printf("-1\n");
  } else
    printf("%d\n", g);
  return 0;
}
