#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll n,a[509],c[509];
ll calc(ll a,ll b)
{
if(b>a)return calc(b,a);
ll t;
while(b)
{
t=a%b;
a=b;
b=t;
}
return a;
}
map<ll,ll> m,b;//gcd,sum of c
int main()
{
#define int long long
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>c[i];
int ans=INT_MAX;
for(int i=1;i<=n;i++)
{
m.clear();
if(a[i]==1)ans=min(ans,c[i]);
//else m[a[i]]=c[i];
for(map<int,int>::iterator it=b.begin();it!=b.end();it++)
{
int val=calc(it->first,a[i]);
if(val==1)
ans=min(ans,it->second+c[i]);
if(b.find(val)==b.end())
if(m.find(val)==m.end())
m[val]=it->second+c[i];
else
m[val]=min(m[val],it->second+c[i]);
else
b[val]=min(b[val],it->second+c[i]);
}
if(b.find(a[i])==b.end())
b[a[i]]=c[i];
else b[a[i]]=min(b[a[i]],c[i]);
for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
b[it->first]=it->second;
}
if(ans==INT_MAX)cout<<-1<<endl;
else cout<<ans<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmxsIG4sYVs1MDldLGNbNTA5XTsKbGwgY2FsYyhsbCBhLGxsIGIpCnsKCWlmKGI+YSlyZXR1cm4gY2FsYyhiLGEpOwoJbGwgdDsKCXdoaWxlKGIpCgl7CgkJdD1hJWI7CgkJYT1iOwoJCWI9dDsKCX0KCXJldHVybiBhOwp9Cm1hcDxsbCxsbD4gbSxiOy8vZ2NkLHN1bSBvZiBjCmludCBtYWluKCkgCnsKCSNkZWZpbmUgaW50IGxvbmcgbG9uZwoJY2luPj5uOwoJZm9yKGludCBpPTE7aTw9bjtpKyspY2luPj5hW2ldOwoJZm9yKGludCBpPTE7aTw9bjtpKyspY2luPj5jW2ldOwoJaW50IGFucz1JTlRfT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