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

priority_queue<pair<int, int> > pq1;
priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>> > pq2;

void DEBUG(priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int,int >> > x) {
    while(!x.empty()) {
        cout << x.top().first << " " << x.top().second << "\n";
        x.pop();
    }
}

int main () { 
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    long long sol = 0;

    for (int i = 0 ; i < n ; i++) {
        int a, b;
        cin >> a >> b;
        pq1.push({a, b});
    }

    // cout << endl << endl;

    for(int i = n ; i > 0 ; i--) {
        auto x = pq1.top();
        // cout << x.first << " " << x.second << " " << i << "\n";
        pq1.pop();
        sol += abs(i - x.first);
        x.first = i;
        pq2.push({x.second, x.first});
    }

    // cout << sol << "\n";

    // cout << endl << endl;

    for(int i = 1 ; i <= n ; i++) {
        auto x = pq2.top();
        // cout << x.first << " " << x.second << " " << i << "\n";
        pq2.pop();
        sol += abs(i - x.first);
    }

    cerr << sol << "\n";

    // DEBUG(pq2);

    return 0;
}