#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int M = 27;
const int inf = 0x7fffffff;
const long long INF = 9e18;
int l[maxn], c[maxn];
map<int, int> dp;
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", l + i);
  for (int i = 0; i < n; i++) scanf("%d", c + i);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int num = l[i], co = c[i];
    for (auto it : dp) {
      int tmp = gcd(it.first, num);
      if (dp[tmp])
        dp[tmp] = min(dp[tmp], it.second + co);
      else
        dp[tmp] = it.second + co;
    }
  }
  if (dp[1])
    cout << dp[1] << endl;
  else
    puts("-1");
  return 0;
}
