#include <bits/stdc++.h>
using namespace std;
int l[302], c[302];
vector<int> num;
map<int, int> dp;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n, i, j;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", &l[i]);
  for (j = 1; j <= n; ++j) scanf("%d", &c[j]);
  for (i = 1; i <= n; ++i) {
    if (dp.count(l[i]))
      dp[l[i]] = min(dp[l[i]], c[i]);
    else {
      dp[l[i]] = c[i];
      num.push_back(l[i]);
    }
  }
  for (i = 1; i <= n; ++i)
    for (j = 0; j < num.size(); ++j) {
      int temp = gcd(l[i], num[j]);
      if (dp.count(temp))
        dp[temp] = min(dp[temp], dp[l[i]] + dp[num[j]]);
      else {
        dp[temp] = dp[l[i]] + dp[num[j]];
        num.push_back(temp);
      }
    }
  if (dp.count(1))
    printf("%d\n", dp[1]);
  else
    puts("-1");
  return 0;
}
