#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); }
int arr[350];
int cost[350];
int n;
map<pair<int, int>, int> dp;
int solve(int loc, int res) {
  if (loc == n) return (res == 1) ? 0 : 1000000010;
  if (dp.find(make_pair(loc, res)) != dp.end()) return dp[make_pair(loc, res)];
  return dp[make_pair(loc, res)] =
             min(solve(loc + 1, res),
                 (cost[loc] + solve(loc + 1, gcd(res, arr[loc]))));
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) cin >> cost[i];
  int ans = 1000000010;
  for (int i = 0; i < n; i++) ans = min(ans, cost[i] + solve(i + 1, arr[i]));
  if (ans == 1000000010)
    cout << -1;
  else
    cout << ans;
}
