#include <bits/stdc++.h>
using namespace std;
int L[310], C[310], n;
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
map<int, int> mp[2];
map<int, int>::iterator it;
int now = 1, pre = 0;
void update(int cur, int a, int b) {
  if (mp[cur].count(a))
    mp[cur][a] = min(mp[cur][a], b);
  else
    mp[cur][a] = b;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &L[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &C[i]);
  mp[pre][L[1]] = C[1];
  for (int i = 2; i <= n; i++) {
    mp[now].clear();
    mp[now][L[i]] = C[i];
    for (it = mp[pre].begin(); it != mp[pre].end(); it++) {
      int a = it->first, b = it->second;
      update(now, a, b);
      update(now, gcd(a, L[i]), b + C[i]);
    }
    swap(now, pre);
  }
  if (mp[pre].count(1))
    printf("%d\n", mp[pre][1]);
  else
    printf("-1\n");
  return 0;
}
