#include <bits/stdc++.h>
using namespace std;
int l[333];
int c[333];
int cvt[100010];
int dp[333][100010];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
map<int, int> mp;
int gcdidx;
int tmpidx;
int crtmp(int g) {
  if (mp.find(g) == mp.end()) {
    cvt[gcdidx] = g;
    mp[g] = gcdidx;
    return gcdidx++;
  } else {
    return mp[g];
  }
}
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]);
  memset(dp, 0x3f, sizeof(dp));
  crtmp(0);
  crtmp(l[0]);
  dp[0][0] = 0;
  dp[0][1] = c[0];
  for (int i = 1; i < n; i++) {
    tmpidx = gcdidx;
    for (int j = 0; j < tmpidx; j++) {
      int cur = gcd(cvt[j], l[i]);
      int idx = crtmp(cur);
      dp[i][idx] = min(dp[i][idx], dp[i - 1][j] + c[i]);
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
    int idx = crtmp(l[i]);
    dp[i][idx] = min(dp[i][idx], c[i]);
  }
  if (mp.find(1) == mp.end())
    cout << -1 << endl;
  else
    cout << dp[n - 1][mp[1]] << endl;
  return 0;
}
