#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9;
const int N = 333;
int n, l[N], c[N];
map<int, int> F[N];
int GCD(int x, int y) {
  if (y == 0) return x;
  return GCD(y, x % y);
}
int dp(int i, int _GCD) {
  if (_GCD == 1) return 0;
  if (i == n + 1) return oo;
  if (F[i].count(_GCD)) return F[i][_GCD];
  int res = dp(i + 1, _GCD);
  res = min(res, dp(i + 1, GCD(_GCD, l[i])) + c[i]);
  return F[i][_GCD] = res;
}
int main(void) {
  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);
  int ans = dp(1, 0);
  if (ans < oo)
    printf("%d", ans);
  else
    printf("-1");
  return 0;
}
