#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const ll INF = 1000000000000000LL;

ll a[100005];
ll b[100005];
ll bit[200005];
int n;
pair <ll, ll> niz[100005];

ll bres = INF;

/*void sim(deque <int> q){

}*/

/*void brutuj(int i, deque <int> q){
    deque <int> q1 = q;
    deque <int> q2 = q;
    q1.push_front(i);
    q2.push_back(i);
    if(i == n){
        sim(q1);
        sim(q2);
        return;
    }
    brutuj(i+1, q1);
    brutuj(i+1, q2);
}

ll brut(){
    brutuj(1, {});
}*/

ll findbit(int x){
    ll res = 0;
    while(x){
        res += bit[x];
        x -= x & -x;
    }
    return res;
}

void addbit(int x, int val){
    while(x < 200005){
        bit[x] += val;
        x += x & -x;
    }
}

pair <int, int> sta[2000005];

long long Resi(int N, int *A, int *B){
    n = N;
    for(int i=1; i<=n; i++){
        a[i] = A[i-1] + 1;
        b[i] = B[i-1] + 1;
        niz[i].first = A[i-1] + 1;
        niz[i].second = B[i-1] + 1;
    }
    /*if(N <= 20){
        return brut();
    }*/
    sort(niz+1, niz+1+n);
    for(int i=1; i<=n; i++){
        sta[niz[i].first].first = 0;
        sta[niz[i].second].first = 1;
        sta[niz[i].first].second = i;
        sta[niz[i].second].second = i;
    }
    ll res = 0;
    int m = 0;
    for(int i=1; i<=2*n; i++){
        if(sta[i].first){
            addbit(i+1, -1);
            m--;
            continue;
        }
        ll koji = sta[i].second;
        ll g = 0;
        ll x = findbit(niz[koji].second);
        g = min(m-x, x);
        res += g;
        addbit(niz[koji].second+1, 1);
        m++;
    }
    return res;
}

/*int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

    int N;
    cin >> N;

    int *A = new int[N], *B = new int[N];
    for(int i = 0; i < N; i++)
        cin >> A[i] >> B[i];

    long long res = Resi(N, A, B);
    cout << res << endl;

    delete[] A;
    delete[] B;

    return 0;
}*/