#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }   int main() {     int n, l[300], c[300];     cin >> n;     for (int i = 0; i < n; i++) cin >> l[i];     for (int i = 0; i < n; i++) cin >> c[i];     unordered_map<int, int> um;     for (int i = 0; i < n; i++)       if (um.find(l[i]) != um.end())         um[l[i]] = min(um[l[i]], c[i]);       else         um.emplace(l[i], c[i]);     for (int i = 0; i < n; i++) {       vector<pair<int, int>> ve;       for (auto& elem : um) {         int g = gcd(elem.first, l[i]);         if (um.find(g) != um.end())           um[g] = min(um[g], elem.second + c[i]);         else           ve.emplace_back(g, elem.second + c[i]);       }       for (auto& elem : ve) um.insert(elem);     }     cout << (um[1] ?: -1) << endl;     return 0;   }   