#include <bits/stdc++.h>
using namespace std;
set<int> divi;
int n, a[310], dp[310][20000], dplog[20000], c[310];
vector<int> Div_Combine;
int gcd(int a, int b) {
  while (b) {
    int t = b;
    b = a % b;
    a = t;
  }
  return a;
}
int main() {
  memset(dp, 63, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    for (int j = 1; j * j <= a[i]; j++) {
      if (a[i] % j != 0) continue;
      divi.insert(j);
      divi.insert(a[i] / j);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (set<int>::iterator i = divi.begin(); i != divi.end(); i++)
    Div_Combine.push_back(*i);
  for (int i = 1; i <= n; i++) {
    memset(dplog, 63, sizeof(dplog));
    for (int j = 0; j < Div_Combine.size(); j++) {
      if (Div_Combine[j] == a[i]) {
        dplog[j] = c[i];
        continue;
      }
      int GCD = gcd(Div_Combine[j], a[i]);
      int pos = lower_bound(Div_Combine.begin(), Div_Combine.end(), GCD) -
                Div_Combine.begin();
      dplog[pos] = min(dplog[pos], dp[i - 1][j] + c[i]);
    }
    for (int j = 0; j < Div_Combine.size(); j++)
      dp[i][j] = min(dp[i - 1][j], dplog[j]);
  }
  printf("%d", (dp[n][0] == 1061109567 ? -1 : dp[n][0]));
}
