#include <bits/stdc++.h>
using namespace std;
int n;
int arr[310];
int cost[310];
unordered_map<int, int> dp[310];
int getGCD(int a, int b) {
  if (b == 0) return a;
  return getGCD(b, a % b);
}
int cal(int idx, int gcd) {
  if (idx == n + 1) {
    if (gcd == 1) return 0;
    return 999999999;
  }
  if (dp[idx].find(gcd) != dp[idx].end()) return dp[idx][gcd];
  int f1, f2;
  f1 = cost[idx] + cal(idx + 1, getGCD(arr[idx], gcd));
  f2 = cal(idx + 1, gcd);
  return dp[idx][gcd] = min(f1, f2);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &cost[i]);
  }
  int uu = cal(1, 0);
  if (uu == 999999999)
    printf("-1\n");
  else
    printf("%d\n", uu);
  return 0;
}
