#include <bits/stdc++.h>
using namespace std;
using ll = long long int;

struct UnionFind {
  vector<int> par, s;
  UnionFind(int n) { init(n); }
  void init(int n) {
    s.assign(n, 1); par.resize(n);
    iota(par.begin(), par.end(), 0);
  }
  int find(int x) {
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(s[x] < s[y]) par[x] = y, s[y] = s[x] + s[y];
    else par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return s[find(x)]; }
};

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll g = a; x = 1, y = 0;
  if(b != 0) g = extgcd(b, a%b, y, x), y -= (a/b) * x;
  return g;
}
ll inv(ll a, ll n) {
  ll s, t;
  extgcd(a, n, s, t);
  return (n+s) % n;
}
using PII = pair<ll,ll>;
const double lim = 1.5 * 1e18;
PII crt(const vector<ll>& a, const vector<ll>& b, const vector<ll>& m) {
  PII ret(0, 1);
  // for(int i=0; i<a.size(); ++i) {
  //   ll s = a[i] * ret.second;
  //   ll t = b[i] - a[i] * ret.first;
  //   ll d = __gcd(m[i], s);
  //   if(t%d != 0) return make_pair(-1, -1);
  //   ll u = t / d * inv(s/d, m[i]/d) % (m[i]/d);
  //   ret.first += ret.second * u;
  //   ret.second *= m[i] / d; 
  //   ret.first = (ret.first % ret.second + ret.second) % ret.second;
  // }
  for(int i=0; i<a.size(); ++i) {
    bool ok = false;
    for(int j=0; j<=m[i]; ++j) {
      if((double)ret.first + (double)ret.second*j > lim) break; 
      ll nans = ret.first+ret.second*j;
      if(nans % m[i] == b[i]) {
        ret.first = nans;
        ret.second = ret.second * (m[i] / __gcd(ret.second, m[i]));
        ok = true;
        break;
      }
    }
    // cout << i << " " << ret.first << " " << ret.second << endl;
    if(!ok) return make_pair(-1, -1);
  }
  return ret;
}

signed main() {
  ll n;
  cin >> n;
  vector<ll> p(n), q(n), ord(n);
  for(int i=0; i<n; ++i) cin >> p[i], p[i]--, ord[p[i]] = i;
  for(int i=0; i<n; ++i) cin >> q[i], q[i]--;

  vector<ll> trans(n);
  for(int i=0; i<n; ++i) trans[i] = q[i];

  UnionFind uf(n);
  for(int i=0; i<n; ++i) uf.unite(i, q[i]);
  vector<vector<ll>> group(n);
  for(int i=0; i<n; ++i) group[uf.find(i)].push_back(ord[i]);
  // for(auto &v: group) {
  //   sort(v.begin(), v.end(), [&](ll l, ll r){
  //     return ord[l] < ord[r];
  //   });
  // }

  for(auto v: group) {
    vector<ll> v1, v2;
    for(auto i: v) {
      v1.push_back(i);
      v2.push_back(ord[i]);
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    if(v1 != v2) {
      cout << -1 << endl;
      return 0;
    }
  }

  vector<ll> a, b, m;
  for(auto v: group) {
    if(v.size() == 0) continue;
    sort(v.begin(), v.end());
    // cout << "v: ";
    // for(auto i: v) cout << i << " ";
    // cout << endl;

    bool ok = false;
    map<vector<ll>,ll> mp;
    vector<ll> permut(n, -1);
    vector<ll> vs;
    for(int i=0; i<v.size(); ++i) {
      permut[v[i]] = p[v[i]];
      vs.push_back(p[v[i]]); 
    }
    sort(vs.begin(), vs.end());
    // cout << "vs: ";
    // for(auto i: vs) cout << i << " ";
    // cout << endl;
    vector<ll> sorted(n, -1);
    for(int i=0; i<v.size(); ++i) {
      sorted[v[i]] = vs[i];
    }
    // cout << "sorted: ";
    // for(auto i: sorted) cout << i << " ";
    // cout << endl;
    for(int i=0; i<v.size()*3; ++i) {
      // cout << i << " ( ";
      // for(auto j: permut) cout << j << " ";
      // cout << ")" << endl;
      if(permut == sorted) ok = true;
      if(permut == sorted && mp.find(permut) != mp.end()) {
        if(i-mp[permut] > 1) {
          a.push_back(1);
          b.push_back(mp[permut]);
          m.push_back(i-mp[permut]);
        }
        // cout << "ok=" << ok << endl;
        break;
      }
      mp[permut] = i;

      vector<ll> n_permut(permut);
      for(int j=0; j<v.size(); ++j) {
        n_permut[v[j]] = trans[permut[v[j]]];
      }
      permut = n_permut;
    }

    // cout << "ok=" << ok << endl;
    if(!ok) {
      cout << -1 << endl;
      return 0;
    }
  }

  // for(auto i: a) cout << i << " ";
  // cout << endl;
  // for(auto i: b) cout << i << " ";
  // cout << endl;
  // for(auto i: m) cout << i << " ";
  // cout << endl;

  ll ret = crt(a, b, m).first;
  if(ret != -1) {
    for(int i=0; i<a.size(); ++i) {
      if(ret % m[i] != b[i]) {
        ret = -1;
        break;
      }
    }
  }
  cout << ret << endl;

  return 0;
}


Measured memory_bytes: 3504