#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

lint tmod(lint a, lint p) { return (a %= p) < 0 ? a + p : a; }
lint mod_inv(lint a, lint p) {
    a = tmod(a, p);
    lint b = p, x = 1, u = 0;
    while (b) {
        lint q = a / b;
        swap(a -= q * b, b);
        swap(x -= q * u, u);
    }
    return a == 1 ? tmod(x, p) : -1;
}

bool pre(V<lint>& a, V<lint>& p) {
    assert(a.size() == p.size());
    int n = a.size();
    for (int i = 0; i < n; ++i) {
        a[i] = tmod(a[i], p[i]);
    }
    for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) {
        lint d = __gcd(p[i], p[j]);
        if (a[i] % d != a[j] % d) return false;
        p[i] /= d;
        p[j] /= d;
        while (true) {
            lint e = __gcd(d, p[j]);
            if (e == 1) break;
            p[j] *= e;
            d /= e;
        }
        p[i] *= d;
        a[i] %= p[i];
        a[j] %= p[j];
    }
    return true;
}

lint CRT(const V<lint>& a, const V<lint>& p) {
    int n = a.size();
    lint x = 0;
    V<lint> y(n);
    lint prod = 1;
    for (int i = 0; i < n; ++i) {
        y[i] = tmod(a[i] - x, p[i]);
        for (int j = 0; j < i; ++j) {
            (y[i] *= mod_inv(p[j], p[i])) %= p[i];
        }
        x += prod * y[i];
        prod *= p[i];
    }
    return x;
}

int main() {
    int n; cin >> n;
    V<> p(n); for (auto&& e : p) cin >> e, --e;
    V<> q(n); for (auto&& e : q) cin >> e, --e;
    VV<> v;
    V<bool> used(n);
    for (int i = 0; i < n; ++i) if (!used[i]) {
        V<> nv;
        int t = i;
        while (true) {
            nv.push_back(t);
            t = q[t];
            if (t == i) break;
        }
        for (int e : nv) used[e] = true;
        v.push_back(nv);
    }
    // for (const auto& e : v) {
    //     for (int f : e) cerr << f << ' ';
    //     cerr << endl;
    // }
    V<lint> a, pr;
    for (const auto& y : v) {
        V<> x;
        for (int e : p) if (find(begin(y), end(y), e) != end(y)) {
            x.push_back(e);
        }
        pr.push_back(y.size());
        for (int i = 0; i < pr.back(); ++i) {
            // for (int e : x) {
                // cerr << e << ' ';
            // }
            // cerr << " x\n";
            if (is_sorted(begin(x), end(x))) {
                a.push_back(i);
                break;
            }
            for (auto&& e : x) {
                int j = find(begin(y), end(y), e) - begin(y);
                e = y[(j + 1) % pr.back()];
            }
        }
        if (a.size() < pr.size()) {
            return cout << -1 << endl, 0;
        }
        // cerr << a.back() << ' ' << pr.back() << endl;
    }
    if (!pre(a, pr)) {
        return cout << -1 << endl, 0;
    }
    lint res = CRT(a, pr);
    for (const auto& y : v) {
        int c = res % y.size();
        while (c--) {
            for (auto&& e : p) {
                int j = find(begin(y), end(y), e) - begin(y);
                if (j == (int)y.size()) continue;
                e = y[(j + 1) % y.size()];
            }
        }
    }
    if (!is_sorted(begin(p), end(p))) {
        return cout << -1 << endl, 0;
    }
    cout << res << endl;
}


Measured memory_bytes: 3160