#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,bool ch[]){
    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";cout<<"answer is "<<ans<<"\n";
    if (v[ind].se.se==0){
        q.push_back(v[ind].se.fi);
        int x=bruteforce(q,ind+1,n,ans,ch);
        q.pop_back();
        ch[v[ind].se.fi]=false;


        q.push_front(v[ind].se.fi);
        int y=bruteforce(q,ind+1,n,ans,ch);
        q.pop_front();
        return min(x,y);
    }
    else{
        int rez=0;
        for (auto i:q){
            if (i==v[ind].se.fi)break;
            if (!ch[i])rez++;
        }
        ch[v[ind].se.fi]=true;
        int x=bruteforce(q,ind+1,n,ans+rez,ch);
        ch[v[ind].se.fi]=false;
        return x;
    }
}

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}});
    bool ch[21];
    memset(ch,false,sizeof(ch));
    deque<int>q;
    sort(all(v));
	return bruteforce(q,0,N,0,ch);
}