#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const string LN = "\n";
const double eps = 1e-9;
const double pi = acos(-1.0);
const int INF = (int)2e9 + 7;
const long long LINF = (long long)9e18 + 7;
const long long P = 353251;
const long long P1 = 239017;
const long long P2 = 239017;
const long long MOD = 1e9 + 7;
const long long MOD2 = 1e9 + 9;
int n;
int a[307];
int c[307];
map<int, int> ans[307];
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int get(int k, int was) {
  if (was == 1) return 0;
  if (k == n) {
    if (was == 1) return 0;
    return INF;
  }
  if (ans[k][was] != 0) return ans[k][was];
  int ansnow = c[k] + get(k + 1, a[k]);
  if (was != 1) ansnow = min(ansnow, get(k + 1, was));
  if (was != -1 && was != 1 && get(k + 1, gcd(was, a[k])) != INF)
    ansnow = min(ansnow, c[k] + get(k + 1, gcd(was, a[k])));
  ans[k][was] = ansnow;
  return ansnow;
}
const bool is_testing = 0;
int main() {
  srand('D' + 'E' + 'N' + 'I' + 'S' + 'S' + 'O' + 'N');
  if (is_testing) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  } else {
  }
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> c[i];
  if (n == 1) {
    if (a[0] == 1)
      cout << c[0];
    else
      cout << -1;
    exit(0);
  }
  int aha = get(0, -1);
  if (aha == INF)
    cout << -1;
  else
    cout << aha;
  if (is_testing) {
    cout << LN << 1.0 * clock() / CLOCKS_PER_SEC << LN;
  }
}
