#include <bits/stdc++.h>
using namespace std;
int value[330][2], n;
map<int, int> dp;
int gcd(int a, int b) {
  if (b == 0) return a;
  return a % b == 0 ? b : gcd(b, a % b);
}
int main() {
  int i, j;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%d", &value[i][0]);
    }
    for (i = 1; i <= n; i++) {
      scanf("%d", &value[i][1]);
    }
    dp.clear();
    map<int, int>::iterator it;
    dp[0] = 0;
    for (i = 1; i <= n; i++) {
      int x = value[i][0];
      for (it = dp.begin(); it != dp.end(); it++) {
        int y = it->first;
        int m = gcd(x, y);
        if (dp[m] != 0 && dp[it->first] + value[i][1] > dp[m]) continue;
        dp[m] = dp[it->first] + value[i][1];
      }
    }
    if (dp[1] == 0)
      cout << -1 << endl;
    else
      cout << dp[1] << endl;
  }
  return 0;
}
