There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Output

Print a single integer — the number of inversions in the resulting sequence.

Examples

Input

2
4 2
1 4


Output

4


Input

3
1 6
3 4
2 5


Output

15

Note

In the first sample the sequence is being modified as follows: <image>. It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).
// Don't place your source in a package
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;


// Please name your class Main
public class Main {
    static FastScanner fs=new FastScanner();
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        public String next() {
            while (!st.hasMoreElements())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        int Int() {
            return Integer.parseInt(next());
        }

        long Long() {
            return Long.parseLong(next());
        }

        String Str(){
            return next();
        }
    }


    public static void main (String[] args) throws java.lang.Exception {
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

        int T=1;
        for(int t=0;t<T;t++){
            int n=Int();
            int A[][]=new int[n][2];
            for(int i=0;i<n;i++){
                A[i][0]=Int()-1;
                A[i][1]=Int()-1;
            }

            Solution sol=new Solution(out);
            sol.solution(A);
        }
        out.close();

    }


    public static int Int(){
        return fs.Int();
    }
    public static long Long(){
        return fs.Long();
    }
    public static String Str(){
        return fs.Str();
    }

}






class Solution{
    PrintWriter out;
    public Solution(PrintWriter out){
        this.out=out;
    }


    long res=0;
    public void solution(int A[][]){
        TreeMap<Integer,Integer>tree=new TreeMap<>();
        for(int p[]:A){
            int a=p[0],b=p[1];
            if(!tree.containsKey(a))tree.put(a,a);
            if(!tree.containsKey(b))tree.put(b,b);

            int temp=tree.get(a);
            tree.put(a,tree.get(b));
            tree.put(b,temp);
        }
        inversePair(tree);


        /*List<Integer>list=new ArrayList<>();
        for(Integer pos:tree.keySet()){
            list.add(tree.get(pos));
        }
        Collections.sort(list);


        TreeMap<Integer,Integer>f=new TreeMap<>();
        for(int i=0;i<list.size();i++){
            f.put(tree.get(i),i);
        }*/

        TreeMap<Integer,Integer>f=new TreeMap<>();
        int j=0;
        for(Integer pos:tree.keySet()){
            f.put(pos,j++);
        }

        //System.out.println(tree);




        for(Integer pos:tree.keySet()){
            int val=tree.get(pos);
            int l1=val+1,r1=pos-1;
            if(l1<=r1){
                int cnt=r1-l1+1;
                cnt-=range(f,l1,r1);
                res+=cnt;
            }

            int l2=pos+1,r2=val-1;
            if(l2<=r2){
                int cnt=r2-l2+1;
                cnt-=range(f,l2,r2);
                res+=cnt;
            }

            //System.out.println(pos+"  "+l1+"  "+r1+"        "+l2+" "+r2);

        }

        //System.out.println();
        //System.out.println(f);
        //System.out.println(range(f,1,2));

        out.println(res);


    }

    public int range(TreeMap<Integer,Integer>f,int l,int r){
        Integer ceil=f.ceilingKey(l);
        if(ceil==null||ceil>r)return 0;
        Integer floor=f.floorKey(r);
        return f.get(floor)-f.get(ceil)+1;
    }

    public void inversePair(TreeMap<Integer,Integer>tree){
        int A[]=new int[tree.size()];
        int j=0;
        Inverse in=new Inverse();
        for(Integer key:tree.keySet()){
            A[j++]=tree.get(key);
        }
        res+=in.solve(A);
    }


}

class Inverse {
    public long solve(int[] A) {
        int id=0;
        Map<Integer,Integer>f=new HashMap<>();
        List<Integer>list=new ArrayList<>();

        for(int i:A){
            list.add(i);
        }

        Collections.sort(list);
        for(int i:list){
            if(!f.containsKey(i)){
                f.put(i,id++);
            }
        }

        int arr[]=new int[f.size()+10];
        FenWick fen=new FenWick(arr);


        for(int i=0;i<A.length;i++){
            A[i]=f.get(A[i]);
            fen.update(A[i],1);
        }

        for(int i=0;i<A.length;i++){
            fen.update(A[i],-1);
            if(A[i]==0)continue;
            A[i]=fen.sumRange(0,A[i]-1);
        }

        long sum=0;
        for(int i:A){
            sum+=i;
        }

        return sum;
    }

    class FenWick {
        int tree[];//1-index based
        int A[];
        int arr[];
        public FenWick(int[] A) {
            this.A=A;
            arr=new int[A.length];
            tree=new int[A.length+1];
            int sum=0;
            for(int i=0;i<A.length;i++){
                update(i,A[i]);
            }
        }

        public void update(int i, int val) {
            arr[i]=+val;
            i++;
            while(i<tree.length){
                tree[i]+=val;
                i+=(i&-i);
            }
        }

        public int sumRange(int i, int j) {
            return pre(j+1)-pre(i);
        }

        public int pre(int i){
            int sum=0;
            while(i>0){
                sum+=tree[i];
                i-=(i&-i);
            }
            return sum;
        }
    }

}












/*
                             ;\
                            |' \
         _                  ; : ;
        / `-.              /: : |
       |  ,-.`-.          ,': : |
       \  :  `. `.       ,'-. : |
        \ ;    ;  `-.__,'    `-.|
         \ ;   ;  :::  ,::'`:.  `.
          \ `-. :  `    :.    `.  \
           \   \    ,   ;   ,:    (\
            \   :., :.    ,'o)): ` `-.
           ,/,' ;' ,::"'`.`---'   `.  `-._
         ,/  :  ; '"      `;'          ,--`.
        ;/   :; ;             ,:'     (   ,:)
          ,.,:.    ; ,:.,  ,-._ `.     \""'/
          '::'     `:'`  ,'(  \`._____.-'"'
             ;,   ;  `.  `. `._`-.  \\
             ;:.  ;:       `-._`-.\  \`.
              '`:. :        |' `. `\  ) \
      -hrr-      ` ;:       |    `--\__,'
                   '`      ,'
                        ,-'


                      free bug dog
*/

