#include <bits/stdc++.h>
using namespace std;
const int power[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
int n, l[310], c[310], ans, tmp, num, line[12];
int dp[550];
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]);
  ans = 655333000;
  for (int i = 1; i <= n; i++) {
    num = 0;
    tmp = l[i];
    for (int j = 2; j * j <= l[i]; j++)
      if (tmp % j == 0) {
        line[++num] = j;
        while (tmp % j == 0) tmp /= j;
      }
    if (tmp > 1) line[++num] = tmp;
    dp[0] = c[i];
    for (int j = 1; j < power[num]; j++) dp[j] = 655333000;
    for (int j = 0; j < power[num] - 1; j++) {
      for (int k = 1; k <= n; k++) {
        int ki = 0;
        for (int w = 1; w <= num; w++)
          if (l[k] % line[w]) ki |= power[w - 1];
        dp[j | ki] =
            ((dp[j | ki]) < (dp[j] + c[k]) ? (dp[j | ki]) : (dp[j] + c[k]));
      }
    }
    ans = ((ans) < (dp[power[num] - 1]) ? (ans) : (dp[power[num] - 1]));
  }
  if (ans == 655333000)
    printf("-1");
  else
    printf("%d", ans);
  return 0;
}
