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).
import org.omg.CORBA.INTERNAL;

import java.io.*;
import java.util.*;

public class P301E{

    static  int bs(int a[],int l  , int r,int x){
        if(l<=r){
            int mid = (l+r)/2;

            if(mid == r){
                if(a[mid]<=x){
                    return mid;
                }

                return bs(a,l,mid-1,x);
            }
            //System.out.println(4+" "+ a[mid] +" "+ a[mid+1] + " "+x);
            if(mid>l && a[mid-1]<x && a[mid] >= x && a[mid+1]>x){
              //  System.out.println(2+"  "+ mid + " "+ r);
                return mid;
            }
            if(mid==l  && a[mid] >= x && a[mid+1]>x){
                //  System.out.println(2+"  "+ mid + " "+ r);
                return mid;
            }

            if(a[mid]>=x){
                //System.out.println(3+"  "+ l + " "+ (mid-1));
                return bs(a,l,mid-1,x);
            }
            //System.out.println(1+"  "+ (mid+1) + " "+ r);
            return bs(a,mid+1,r,x);
        }

        return  -1;
    }

    static long ans = 0;
    static long count_inv(int a[],int l,int r){

        if(l<r){
            int mid = (l+r)/2;
            count_inv(a,l,mid);
            count_inv(a,mid+1,r);

           ans +=  merge(a,l,r);
        }

        return ans;
    }


    private static long merge(int[] a, int l, int r) {
         int mid = (l+r)/2;
         int left[] = new int[mid - l + 1];
         int right[] = new int[r - mid];

         for(int i=0;i<left.length;i++){
             left[i] = a[i+l];
         }

         for(int i=0;i<right.length;i++){
             right[i] = a[mid+1+i];
         }

         int k = l;
         int i =0;
         int j = 0;
         long count = 0;
         while(i<left.length && j<right.length){

             if(left[i]<right[j]){
                 a[k++] = left[i++];
             }
             else{
                 a[k++] = right[j++];
                 count += left.length - i;
             }
         }

         while (i<left.length){
             //count += right.length;
             a[k++] = left[i++];
         }

         while(j<right.length){
             a[k++] = right[j++];
         }

         //System.out.println(l+" "+r +" "+count);
           return count;
    }

    public static void main(String args[]){

        Reader s = new Reader();
        int q = s.nextInt();
        TreeMap<Integer,Integer> map = new TreeMap<>();

        for(int i=0;i<q;i++){
            int x  = s.nextInt() - 1;
            int y = s.nextInt() - 1;

            if(!map.containsKey(x) && !map.containsKey(y)){
                if(x!=y) {
                    map.put(x, y);
                    map.put(y, x);
                }
            }
            else if(!map.containsKey(x)){


                    map.put(x, map.get(y));


                    map.put(y, x);


            }
            else if(!map.containsKey(y)){

                    map.put(y, map.get(x));


                    map.put(x, y);


            }
            else{
                int tx = map.get(x);

                    map.put(x,map.get(y));


                    map.put(y,tx);
            }
        }

        List<Integer> temp = new ArrayList<>();

        for(Map.Entry<Integer,Integer> val:map.entrySet()) {

            if(val.getValue() == val.getKey()){
                temp.add(val.getKey());
            }

        }

        for(int c:temp){
            map.remove(c);
        }

            // System.out.println(map.size());
        int a[] = new int[map.size()];
        int b[] = new int[map.size()];
        int i = 0;
        for(Map.Entry<Integer,Integer> val:map.entrySet()){
            //System.out.println((1+val.getKey())+" "+(1+val.getValue()));
            a[i] =  val.getValue();
            b[i] = val.getKey();
            i++;
        }


        for(i=0;i<a.length;i++){
          // System.out.print(a[i]+" ");
        }
        //System.out.println();
        long ans =  count_inv(a,0,a.length-1);

        for(i=0;i<a.length;i++){
           // System.out.print(a[i]+" ");
        }
        //System.out.println();

        //System.out.println(ans);
        //bs(a,0,a.length-1,4);
        for(Map.Entry<Integer,Integer> val:map.entrySet()){
            //System.out.println(val.getValue()+" "+bs(a,0,a.length-1,val.getValue()));

            if(val.getValue()>val.getKey()){
                int dif = val.getValue() - val.getKey();
               // System.out.println(val.getValue() + " "+val.getKey());
                int df2 = bs(a,0,a.length-1,val.getValue()) - bs(a,0,a.length-1,val.getKey());
                ans += (dif-df2);
            }
            else{
                int dif = val.getKey() - val.getValue();
                int df2 = bs(a,0,a.length-1,val.getKey()) - bs(a,0,a.length-1,val.getValue()) ;

                ans += (dif-df2);

            }
        }

        System.out.println(ans);

    }
    static class Reader {

        private InputStream mIs;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public Reader() {
            this(System.in);
        }

        public Reader(InputStream is) {
            mIs = is;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();

            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = mIs.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

}
