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

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(),std::find(x.begin(),x.end(),0));
        m += abs((i.first-1-j));
        x[i.first-1]--;
        x[j]++;
    }
    if(y[i.second-1]>1){
        j = std::distance(y.begin(),std::find(y.begin(),y.end(),0));
        m += abs((i.second-1-j));
        y[i.second-1]--;
        y[j]++;
    }
}
std::cout << m;
}