#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
map<int, int> ma;
int l[305], c[305];
int dp[maxn], g[maxn];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n, cnt = 0;
  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]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= cnt; ++j) {
      int t = gcd(g[j], l[i]);
      if (!ma.count(t)) {
        g[++cnt] = t;
        ma[t] = cnt;
        dp[cnt] = dp[j] + c[i];
      } else {
        dp[ma[t]] = min(dp[ma[t]], dp[j] + c[i]);
      }
    }
    if (!ma.count(l[i])) {
      g[++cnt] = l[i];
      ma[l[i]] = cnt;
      dp[cnt] = c[i];
    } else {
      dp[ma[l[i]]] = min(dp[ma[l[i]]], c[i]);
    }
  }
  if (ma[1] == 0)
    puts("-1");
  else
    printf("%d\n", dp[ma[1]]);
  return 0;
}
