#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<utility>
#include<numeric>
#include<cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define int long long
#define MAX_N 100005
typedef long long ll;

class UnionFind{
    private:
    int sz;
    vector<int> par,size_;
    public:
    UnionFind(){}
    UnionFind(int node_size):sz(node_size),par(sz),size_(sz,1){
        iota(par.begin(),par.end(),0);
    }
    int find(int x){
        if(par[x]==x)return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x),y = find(y);
        if(x==y)return;
        if(size_[x]<size_[y])swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
};

ll Gcd(ll a,ll b){
    if(a%b==0){
        return b;
    }else{
        return Gcd(b,a%b);
    }
}

ll extgcd(ll a,ll b,ll &x,ll &y){
    if(b!=0){
        extgcd(b,a%b,y,x);
        y -= (a/b)*x;
    }else{
        x = 1;
        y = 0;
    }
}

pair<ll,ll> CRT(const pair<ll,ll>&a1,const pair<ll,ll>&a2){
    const ll v1 = a1.first,m1 = a1.second;
    const ll v2 = a2.first,m2 = a2.second;
    ll g = Gcd(m1,m2);
    ll dev = (v2-v1)/g;
    ll mod = abs(v2-v1)%g;
    if(mod) return make_pair((ll)-1,(ll)-1);
    ll x,y;
    extgcd(m1/g,m2/g,x,y);
    x *= dev, y *= dev;
    const ll m = m1/g*m2;
    return make_pair(((((__int128)m1*x+v1)%m)+m)%m,m);
}

ll calc(vector<int>x,vector<int>&p,vector<int>&q){
    // vector<int>inv(500);
    // rep(i,p.size()){
    //     inv[p[i]] = i;
    // }
    ll s = 0;
    ll cur = x[0];
    ll id;
    rep(i,p.size()){
        if(p[i]==x[0]){
            id = i;
        }
    }
    ll tar = id;
    ll res = 0;
    //cerr << cur << " " << tar << endl;
    while(cur!=tar){
        cur = q[cur];
        res++;
    }
    return res;
}

signed main(){
    int n;
    cin >> n;
    vector<int>p(n),q(n);
    vector<int>inv(n);
    rep(i,n){
        cin >> p[i];
        p[i]--;
        inv[p[i]] = i;
    }
    rep(i,n){
        cin >> q[i];
        q[i]--;
    }
    vector<bool> a(n);
    UnionFind uf(n);
    rep(i,n){
        uf.unite(i,q[i]);
    }
    map<int,vector<int> > vv;
    rep(i,n){
        vv[uf.find(q[i])].push_back(i);
    }
    vector<vector<int> > v;
    for(auto x:vv){
        v.push_back(x.second);
        // for(auto xx:v.back()){
        //     cerr << xx << " " << inv[xx] << endl;
        // }
        // cerr << endl;
    }
    for(auto x:v){
        vector<int> pppp = x;
        sort(pppp.begin(),pppp.end());
        vector<int> y;
        for(auto ss:x){
        //    cerr << ss << " " << inv[ss] << endl;
            y.push_back(inv[ss]);
        }
        sort(y.begin(),y.end());
        for(int i=0;i<x.size();i++){
          //  cerr << pppp[i] << " " << y[i] << endl;
            if(pppp[i]!=y[i]){
            //    cerr << "test" << endl;
                cout << -1 << endl;
                return 0;
            }
        }
    }
    pair<ll,ll> res = make_pair((ll)0,(ll)0);
    for(auto x:v){
        if(x.size()==1)continue;
        int id;
        rep(i,p.size()){
            if(p[i]==x[0])id = i;
        }
        int cur = x[0];
        vector<int> c = p;
        int aa = 0;
        while(cur!=id){
            cur = q[cur];
            rep(i,n){
                c[i] = q[c[i]];
            }
            aa++;
        }
        rep(i,x.size()){
            if(c[x[i]]!=x[i]){
                cout << -1 << endl;
                return 0;
            }
        }
        pair<ll,ll> sss= make_pair(aa,x.size());
        // for(auto tt:y){
        //     cerr << tt << " ";
        // }
        // cerr << endl;
        // cerr << endl;
        // cerr << sss.first << " " << sss.second  << endl;
        if(res==make_pair((ll)0,(ll)0)){
            res = sss;
        }else{
            res = CRT(res,sss);
        }
        //cerr << res.first << " " << res.second << endl;
        if(res==make_pair((ll)-1,(ll)-1)){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << res.first << endl;
    return 0;
}


Measured memory_bytes: 3172