#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 brutuj(int i, vector <int> vec){

}

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){
    while(x < 200005){
        bit[x]++;
        x += x & -x;
    }
}

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);
    ll res = 0;
    for(int i=1; i<=n; i++){
        ll g = 0;
        ///stavi iza
        g = i-1;
        ///stavi ispred
        ll x = findbit(niz[i].second);
        g = min(g, i-1-x);
        res += g;
        addbit(niz[i].second+1);
    }
    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;
}