#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>

using namespace std;

struct Dolazak
{
    int vreme;
    int rb;
};

bool cmp(Dolazak d1, Dolazak d2)
{
    return (d1.vreme<d2.vreme);
}

long long Resi(int N, int *A, int *B)
{
    vector<Dolazak> a(N);
    for(int i=0;i<N;i++)
    {
        a[i].vreme=A[i];
        a[i].rb=i;
    }
    vector<int> elementi;
    sort(a.begin(),a.end(),cmp);
    deque<int> dek;
    for(int j=0;j<N;j++)
    {
        int brojac=0;
        for(int i=0;i<elementi.size();i++)
        {
            if(B[elementi[i]]<B[a[j].rb])
            {
                brojac++;
                //cout << "?";
            }
            //cout << B[elementi[i]] << " " << B[a[j].rb] << endl;
        }
        //cout << brojac << "*\n";
        if(brojac>j+1-brojac)
        {
            dek.push_back(a[j].rb);
            //cout << '!' << endl;
        }
        else
        {
            dek.push_front(a[j].rb);
            //cout << "?" << endl;
        }
        elementi.push_back(a[j].rb);
    }
    /*for(int i=0;i<N;i++) cout << dek[i] << ' ';
    cout << endl;*/
    long long minn=0;
    for(int i=0;i<N;i++)
    {
        int tr=i;
        for(int j=0;j<i;j++)
        {
            if(A[dek[j]]>B[dek[i]] || A[dek[i]]<A[dek[j]])
            {
                tr--;
                //cout << "!";
            }
        }
        //cout << tr << endl;
        minn+=tr;
    }
    return minn;
}