import java.util.Scanner;

public class Zlatnici {

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner scan = new Scanner(System.in);
            int N = scan.nextInt();
            int[] kolone = new int[N];
            int[] vrste = new int[N];
            for (int i = 0; i < N; i++) {
                    int a = scan.nextInt();
                    int b = scan.nextInt();
                    vrste[a - 1]++;
                    kolone[b - 1]++;
            }
            long n = 0;
            for (int i = 0; i < N - 1; i++) {

                    if (vrste[i] != 1) {
                            vrste[i + 1] = vrste[i + 1] + vrste[i] - 1;

                            n = n + Math.abs(vrste[i] - 1);

                    }
                    if (kolone[i] != 1) {
                            kolone[i + 1] = kolone[i + 1] + kolone[i] - 1;

                            n = n + Math.abs(kolone[i] - 1);

                    }

            }

            System.out.println(n);
    }

}