#include <bits/stdc++.h>
using namespace std;
int n, ans = 1000000007;
vector<int> fact[307];
int c[307];
int l[307];
int dp[1 << 10];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i];
    int curr = l[i];
    for (int j = 2; j * j <= l[i]; ++j) {
      if (curr % j == 0) {
        while (curr % j == 0) curr /= j;
        fact[i].push_back(j);
      }
    }
    if (curr > 1) fact[i].push_back(curr);
  }
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; ++i) {
    memset(dp, 0x3f3f3f3f, sizeof dp);
    int d = fact[i].size();
    dp[(1 << d) - 1] = c[i];
    for (int j = i + 1; j <= n; ++j) {
      for (int m = 0; m < (1 << d); ++m) {
        int curr = m;
        for (int k = 0; k < d; ++k) {
          if (l[j] % fact[i][k]) curr &= ~(1 << k);
        }
        dp[curr] = min(dp[curr], dp[m] + c[j]);
      }
    }
    ans = min(ans, dp[0]);
  }
  if (ans == 1000000007)
    cout << -1;
  else
    cout << ans;
}
