#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;

int main()
{ ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, n, x, y, k, sum;
  cin>> n;
  vi veka(n), ceka(n);
  for(i=0; i<n; i++)
  { cin>> x>> y;
    veka[x-1]++; ceka[y-1]++;
  }
  sum=0;
  for(i=0; i<n; i++)
  { if(veka[i]==0){veka[i+1]--; sum+=1;}
    else if(veka[i]>1){veka[i+1]+=veka[i]-1; sum+=veka[i]-1;}
    else if(veka[i]<0){veka[i+1]+=veka[i]-1; sum+=-veka[i]+1;}
  }
  for(i=0; i<n; i++)
  { if(ceka[i]==0){ceka[i+1]--; sum+=1;}
    else if(ceka[i]>1){ceka[i+1]+=ceka[i]-1; sum+=ceka[i]-1;}
    else if(ceka[i]<0){ceka[i+1]+=ceka[i]-1; sum+=-ceka[i]+1;}
  }
  cout<< sum;

  return 0;
}