#include <bits/stdc++.h>

#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define mod 1000000007
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back

using namespace std;

vector<pair<int,pair<int,int>>>a,b,v;

int bruteforce(deque<int>q,int ind,int n,int ans){
    if (ind==2*n)return ans;

    //cout<<"Indices "<<ind<<" d or u? "<<v[ind].se.se<<" "<<v[ind].se.fi<<" ->";for (auto i:q)cout<<i<<" ";cout<<"\n";
    if (v[ind].se.se==0){
        q.push_back(v[ind].se.fi);
        int x=bruteforce(q,ind+1,n,ans);
        q.pop_back();

        q.push_front(v[ind].se.fi);
        int y=bruteforce(q,ind+1,n,ans);
        q.pop_front();
        return min(x,y);
    }
    else{
        //cout<<v[ind].se.fi<<" : ";for (auto i:q)cout<<i<<" ";cout <<" - ";
        int rez=0;
        vector<int>vr;
        for (auto i:q){
            if (i!=v[ind].se.fi)rez++,vr.pb(i);
            else break;
        }

        if (rez==q.size())return 1e9;
        for (int i=0;i<=rez;i++)q.pop_front();
        reverse(all(vr));
        for (auto i:vr)q.push_front(i);

        //for (auto i:q)cout<<i<<" ";cout<<"\n";

        return bruteforce(q,ind+1,n,ans+rez);
    }
}

long long Resi(int N, int *A, int *B) {
    for (int i=0;i<N;i++)v.pb({A[i],{i,0}});
    for (int i=0;i<N;i++)v.pb({B[i],{i,1}});
    deque<int>q;
    sort(all(v));
	return bruteforce(q,0,N,0);
}