#include <bits/stdc++.h>
using namespace std;
const int MAXN = 310;
const int inf = (int)1e5 * MAXN;
int n;
map<int, int> dp[MAXN];
map<int, bool> bio[MAXN];
pair<int, int> arr[MAXN];
inline int Gcd(int a, int b) {
  if (!b) return a;
  return Gcd(b, a % b);
}
int Rek(int ind, int gcd_sada) {
  if (gcd_sada == 1) return 0;
  if (ind == n) return inf;
  if (bio[ind][gcd_sada]) return dp[ind][gcd_sada];
  bio[ind][gcd_sada] = true;
  int dalje = Rek(ind + 1, gcd_sada);
  int novi = Rek(ind + 1, Gcd(gcd_sada, arr[ind].first)) + arr[ind].second;
  return dp[ind][gcd_sada] = min(dalje, novi);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i].first);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i].second);
  int sol = Rek(0, 0);
  printf("%d\n", sol == inf ? -1 : sol);
  return 0;
}
