#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct slog {
ll nule; ll vise;
};
slog v2[200005],k2[200005];
ll n,v[200005],k[200005],a,b,brn,brv,brn2,brv2,res;
int main () {
    brv=1;
    brv2=1;
scanf("%lld",&n);
for (int i=1;i<=n;i++){
    scanf("%lld%lld",&a,&b);
    v[a]++;
    k[b]++;
}
for (int i=1;i<=n;i++){
   if (v[i]==0){
    brn++;
    v2[brn].nule=i;
   }
   if (v[i]>1){
     for (int j=brv;j<=brv+v[i]-1;j++){
        v2[j].vise=i;
     }
     brv+=v[i]-1;
   }

   if (k[i]==0){
    brn2++;
    k2[brn2].nule=i;
   }
   if (k[i]>1){
     for (int j=brv2;j<=brv2+k[i]-1;j++){
        k2[j].vise=i;
     }
     brv2+=k[i]-1;
   }
}
for (int i=1;i<=brn;i++){
    res+=abs(v2[i].nule-v2[i].vise);
}
for (int i=1;i<=brn2;i++){
    res+=abs(k2[i].nule-k2[i].vise);
}
printf("%lld\n",res);
return 0;
}