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

std::vector<int>::iterator findl(std::vector<int>::iterator c){
std::vector<int>::iterator l = c;
std::vector<int>::iterator r = c;
for(;;--l,++r){
    if(*l == 0)
        return l;
    if(*r == 0)
        return r;
}
}

int main(){
    std::ios_base::sync_with_stdio(false);
    int n,xt,yt,m,j;std::cin >> n;
std::vector <int> x(n,0);
std::vector <int> y(n,0);
std::vector <std::pair<int,int> > v(n);
for(int i = 0;i < n;i++){
    std::cin >> xt >> yt;
    v[i] = std::pair<int,int>{xt,yt};
    ++x[xt-1];
    ++y[yt-1];
}
m = 0;
for(auto i:v){
    if(x[i.first-1]>1){
        j = std::distance(x.begin(),findl(x.begin()+(i.first-1)));
        m += abs((i.first-1-j));
        x[i.first-1]--;
        x[j]++;
    }
    if(y[i.second-1]>1){
        j = std::distance(y.begin(),findl(y.begin()+(i.second-1)));
        m += abs((i.second-1-j));
        y[i.second-1]--;
        y[j]++;
    }
}
std::cout << m;
}