#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<list>

void input(std::vector< std::pair<int, int> >&,
           std::vector< std::pair<int, int> >&);
int calc1d(std::vector< std::pair<int, int> >&);
int calc(std::vector< std::pair<int, int> >&,
         std::vector< std::pair<int, int> >&);

int main(){
    std::vector< std::pair<int, int> > row;
    std::vector< std::pair<int, int> > col;

    input(row, col);
    std::cout << calc(row, col) << std::endl;

    return 0;
}

void input(std::vector< std::pair<int, int> >& t_row,
           std::vector< std::pair<int, int> >& t_col){
    int n;
    int r, c;
    std::cin >> n;
    for(int i = 0; i < n; ++i){
        t_row.push_back(std::make_pair(0, i));
        t_col.push_back(std::make_pair(0, i));
    }

    for(int i = 0; i < n; ++i){
        std::cin >> r >> c;
        ++t_row[r-1].first;
        ++t_col[c-1].first;
    }
}

int calc1d(std::vector< std::pair<int,int> >& t_arr){
    std::list<int> pos;
    std::list<int> neg;
    int n = t_arr.size();
    int res = 0;

    for(int i = 0; i < n; ++i){
        if(t_arr[i].first < 1){
            if(pos.empty()){
                neg.push_back(t_arr[i].second);
            }
            else{
                res += abs(pos.front() - t_arr[i].second);
                pos.pop_front();
            }
        }
        if(t_arr[i].first > 1){
            while(t_arr[i].first > 1){
                if(neg.empty()){
                    pos.push_back(t_arr[i].second);
                }
                else{
                    res += abs(neg.front() - t_arr[i].second);
                    neg.pop_front();
                }
                --t_arr[i].first;
            }
        }
    }

    return res;
}

int calc(std::vector< std::pair<int, int> >& t_row,
         std::vector< std::pair<int, int> >& t_col){
    return calc1d(t_row) + calc1d(t_col);
}