#include <bits/stdc++.h>
using namespace std;
struct node {
  int data;
  long int sum;
  struct node *left, *right;
  struct node* next;
};
struct node* newNode(int data) {
  struct node* Node = new (struct node);
  Node->data = data;
  Node->left = Node->right = Node->next = NULL;
  return (Node);
}
long long dist[310];
long long cost[310];
int n;
map<long long, long long> m;
long long gcd_(long long a, long long b) {
  if (a < b) {
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
  }
  if (a % b == 0) {
    return b;
  } else {
    return gcd_(b, a % b);
  }
}
long long findgcd() {
  long long gcd = dist[0];
  for (int i = 0; i < n; i++) {
    gcd = gcd_(max(dist[i], gcd), min(dist[i], gcd));
    printf("%I64d\n", gcd);
    ;
  }
  return gcd;
}
void solveTestCase() {
  scanf("%d", &n);
  ;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &dist[i]);
    ;
  }
  for (int i = 0; i < n; i++) {
    scanf("%I64d", &cost[i]);
    ;
  }
  m[0] = 0;
  for (int i = 0; i < n; i++) {
    map<long long, long long>::iterator it;
    for (it = m.begin(); it != m.end(); it++) {
      long long gcd_temp;
      if (it->first == 0) {
        gcd_temp = dist[i];
      } else {
        gcd_temp = gcd_(max(dist[i], it->first), min(dist[i], it->first));
      }
      if (m.find(gcd_temp) != m.end()) {
        m[gcd_temp] = min(m[gcd_temp], cost[i] + it->second);
      } else {
        m[gcd_temp] = cost[i] + it->second;
      }
    }
  }
  long long ans;
  ans = m.find(1) != m.end() ? m[1] : -1;
  printf("%I64d\n", ans);
  ;
}
int main() {
  solveTestCase();
  return 0;
}
