#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const ll mod=1e9+7;
const ll inf = (ll)1e17;
const ld eps = 1e-12;
const ld PI = 3.14159265358979323846;
ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}




int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    vector<int>v1(n),v2(n);
    for(int i=0;i<n;i++){
        cin>>v1[i];
    }
    for(int i=0;i<n;i++){
        cin>>v2[i];
    }
    vector<vector<int>>v;
    if(n==8){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                for(int d=1;d<=n;d++){
                    if(d==c || d==b || d==a)continue;
                    for(int e=1;e<=n;e++){
                        if(e==d || e==c || e==b || e==a)continue;
                        for(int f=1;f<=n;f++){
                            if(f==e || f==d || f==c || f==b || f==a)continue;
                            for(int g=1;g<=n;g++){
                                if(g==f || g==e || g==d || g==c || g==b || g==a)continue;
                                for(int h=1;h<=n;h++){
                                    if(h==a || h==b || h==c ||h==d || h==e|| h==f || h==g)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    w.push_back(d);
                                    w.push_back(e);
                                    w.push_back(f);
                                    w.push_back(g);
                                    w.push_back(h);
                                    v.push_back(w);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    }
    else if(n==7){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                for(int d=1;d<=n;d++){
                    if(d==c || d==b || d==a)continue;
                    for(int e=1;e<=n;e++){
                        if(e==d || e==c || e==b || e==a)continue;
                        for(int f=1;f<=n;f++){
                            if(f==e || f==d || f==c || f==b || f==a)continue;
                            for(int g=1;g<=n;g++){
                                if(g==f || g==e || g==d || g==c || g==b || g==a)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    w.push_back(d);
                                    w.push_back(e);
                                    w.push_back(f);
                                    w.push_back(g);
                                    v.push_back(w);

                            }
                        }
                    }
                }
            }
        }
    }
    }
    else if(n==6){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                for(int d=1;d<=n;d++){
                    if(d==c || d==b || d==a)continue;
                    for(int e=1;e<=n;e++){
                        if(e==d || e==c || e==b || e==a)continue;
                        for(int f=1;f<=n;f++){
                            if(f==e || f==d || f==c || f==b || f==a)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    w.push_back(d);
                                    w.push_back(e);
                                    w.push_back(f);
                                    v.push_back(w);


                        }
                    }
                }
            }
        }
    }
    }
    else if(n==5){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                for(int d=1;d<=n;d++){
                    if(d==c || d==b || d==a)continue;
                    for(int e=1;e<=n;e++){
                        if(e==d || e==c || e==b || e==a)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    w.push_back(d);
                                    w.push_back(e);
                                    v.push_back(w);



                    }
                }
            }
        }
    }
    }
    else if(n==4){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                for(int d=1;d<=n;d++){
                    if(d==c || d==b || d==a)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    w.push_back(d);
                                    v.push_back(w);


                }
            }
        }
    }
    }
    else if(n==3){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
            for(int c=1;c<=n;c++){
                if(c==b || c==a)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    w.push_back(c);
                                    v.push_back(w);



            }
        }
    }
    }
    else if(n==2){
    for(int a=1;a<=n;a++){
        for(int b=1;b<=n;b++){
            if(a==b)continue;
                                    vector<int>w;
                                    w.push_back(a);
                                    w.push_back(b);
                                    v.push_back(w);


            }
    }
    }
    else if(n==1){
    for(int a=1;a<=n;a++){
        vector<int>w;
        w.push_back(a);
        v.push_back(w);
        }
    }

    int a=0;
    int b=0;
    for(int i=0;i<v.size();i++){
        if(v[i]==v1)a=i+1;
        if(v[i]==v2)b=i+1;    }
    cout<<abs(a-b)<<endl;
    return 0;

}


Measured memory_bytes: 3384