#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int LINF = 0x3f3f3f3f3f3f3f3fll;
const long double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 310;
int l[N], c[N];
map<long long int, int> mapa;
int mdc(int a, int b) {
  if (b == 0) return a;
  return mdc(b, a % b);
}
bool add(int x, int custo) {
  if (mapa.count(x)) {
    if (mapa[x] > custo) {
      mapa[x] = custo;
      return true;
    }
    return false;
  } else {
    mapa[x] = custo;
    return true;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &l[i]);
  for (int i = 0; i < n; i++) scanf("%d", &c[i]);
  set<int> q;
  for (int i = 0; i < n; i++) {
    add(l[i], c[i]);
    q.insert(l[i]);
  }
  for (int i = 0; i < 100000; i++) {
    if (q.size() == 0) break;
    int x = *q.begin();
    q.erase(q.begin());
    for (int j = 0; j < n; j++) {
      int valnovo = mdc(x, l[j]);
      int custonovo = c[j] + mapa[x];
      if (add(valnovo, custonovo)) q.insert(valnovo);
    }
  }
  if (mapa.count(1)) {
    printf("%d\n", mapa[1]);
  } else {
    int coco = -1;
    printf("%d\n", coco);
  }
  return 0;
}
