#include <bits/stdc++.h>
using namespace std;
map<int, int> dp;
map<int, int>::iterator it;
int num[305];
int cost[305];
int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }
int main() {
  int n;
  scanf("%d", &n);
  bool ans = true;
  for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &cost[i]);
  int tmp = num[1];
  for (int i = 2; i <= n && tmp != 1; i++) tmp = gcd(tmp, num[i]);
  if (tmp != 1) ans = false;
  if (ans) {
    for (int i = 1; i <= n; i++) {
      int a = num[i];
      int b = cost[i];
      for (it = dp.begin(); it != dp.end(); it++) {
        int c = gcd(a, it->first);
        if (dp.find(c) != dp.end())
          dp[c] = min(dp[c], it->second + b);
        else
          dp[c] = it->second + b;
      }
      if (dp.find(a) != dp.end())
        dp[a] = min(dp[a], b);
      else
        dp[a] = b;
    }
    printf("%d\n", dp[1]);
  } else
    puts("-1");
  return 0;
}
