#include <iostream>

#include <bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;a++)
#define Ford(a,b,c) for(int a=b;a>=c;a--)
#define pb push_back
#define ll long long
#define piii pair<pair<int,int>,int >
#define pii pair<int,int>
#define se second
#define fi first
using namespace std;
const int MAX=2*1e5+5;
pii event[MAX];
int niz[MAX],nizop[MAX],l=0;
piii polozaj[MAX];
int pol[MAX],izbac[MAX],izb[MAX],kolk[MAX],pomoc[MAX];


long long Resi(int N, int *A, int *B)
{
    int n=N;
    ll rez=0,tmp=1e18,u=0;
    int c=0,k=0;
    For(i,0,n-1)
    {
       event[A[i]]={i,0};
       polozaj[i].fi.fi=A[i];
       polozaj[i].se=i;

    }
     For(i,0,n-1)
    {
        event[B[i]]={i,1};
        polozaj[i].fi.se=B[i];
    }
    For(i,0,2*n-1)
    {
        if (event[i].se==1)
         u++;
        izb[i]=u;
    }
    For(i,0,2*n-1)
    {
        if (i==0)
        {
            kolk[i]=1;
        }
        else
        {
            if (event[i].se==1)
            {
                kolk[i]=kolk[i-1]-1;
            }
            else kolk[i]=kolk[i-1]+1;
        }
    }
    if (n<=20)
    {
    For(i,0,(1<<n)-1)
    {
         rez=0; c=0; k=0;
        For(j,0,2*n-1)
        {
            if (event[j].se==0)
            {
                if (i&((1<<c)))
                {
                    niz[k++]=event[j].fi;
                    c++;
                }
                else
                {
                    Ford(t,k,1)
                    {
                        niz[t]=niz[t-1];
                    }
                    niz[0]=event[j].fi;
                    k++;
                    c++;
                }
            }
            else
            {
                For(t,0,k-1)
                {
                    if (niz[t]==event[j].fi)
                    {
                        rez+=t;
                        For(r,t,k-2)
                        niz[r]=niz[r+1];
                        k--;
                        break;
                    }
                }
            }
           /* For(t,0,k-1)
            printf("%i ",niz[t]);
            printf("\n");*/

        }
        //cout<<rez<<endl;
       // cout<<endl;

        tmp=min(tmp,rez);
    }
    //cout<<ras<<endl;
    return tmp;

}
    else
    {
        sort(polozaj,polozaj+n);
        reverse(polozaj,polozaj+n);
       // For(i,0,2*n-1) cout<<kolk[i]<<" ";
        For(i,0,n-1)
        {
            int m=kolk[polozaj[i].fi.se];
            int p=kolk[polozaj[i].fi.fi]-1;
            //cout<<m<<" "<<p<<endl;
            if (m<p)
            {
                rez+=m+pomoc[polozaj[i].se];
            }
            else
            {
                rez+=pomoc[polozaj[i].se];
                For(j,0,n-1)
                {
                    if (polozaj[j].fi.fi<polozaj[i].fi.fi)
                    pomoc[polozaj[j].se]++;
                }
            }


        }
        return rez;
    }
}
/*
int main()
{
    int n,a[MAX],b[MAX];
    cin>>n;
    For(i,0,n-1)
    cin>>a[i];
    For(i,0,n-1)
    cin>>b[i];
    cout<<Resi(n,a,b);
    return 0;
}

4
0 1 2 5
3 7 4 6

5
0 1 2 3 4
8 6 7 5 9
*/