#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return a % b ? gcd(b, a % b) : b; }
int f[50000], dp[50000], cnt;
pair<int, int> a[500];
inline void factor(int x) {
  cnt = 0;
  for (int i = 1; i * i <= x; i++)
    if (x % i == 0) {
      f[++cnt] = i;
      if (i * i != x) f[++cnt] = x / i;
    }
  sort(f + 1, f + cnt + 1);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].second);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].first);
  sort(a + 1, a + n + 1);
  int g = a[1].second, ans = 0x7f7f7f7f;
  for (int i = 2; i <= n; i++) g = gcd(g, a[i].second);
  if (g != 1) return puts("-1"), 0;
  for (int i = 1; i <= n; i++) {
    memset(dp, 0x7f7f7f7f, sizeof(dp));
    factor(a[i].second);
    dp[cnt] = a[i].first;
    for (int j = 1; j <= n; j++)
      if (j != i)
        for (int k = 1; k <= cnt; k++)
          dp[lower_bound(f + 1, f + cnt + 1, gcd(f[k], a[j].second)) - f] = min(
              dp[lower_bound(f + 1, f + cnt + 1, gcd(f[k], a[j].second)) - f],
              dp[k] + a[j].first);
    ans = min(ans, dp[1]);
  }
  printf("%d", ans);
  return 0;
}
