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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    static long res;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = sc.nextInt();
        int[] a = new int[n], b = new int[n];
        int[] rev = new int[2 * ((int)1e5 + 10)];
        HashMap<Integer,Integer> map = new HashMap<>();
        ArrayList<Integer> sorted = new ArrayList<>();
        int mapIdx = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
            if (!map.containsKey(a[i])) {
                sorted.add(a[i]);
                rev[mapIdx] = a[i];
                map.put(a[i], mapIdx++);
            }
            if (!map.containsKey(b[i])) {
                sorted.add(b[i]);
                rev[mapIdx] = b[i];
                map.put(b[i], mapIdx++);
            }
        }

        Collections.sort(sorted);
        HashMap<Integer,Integer> posInSorted = new HashMap<>();
        for (int i = 0; i < sorted.size(); i++)
            posInSorted.put(sorted.get(i), i);

        int[] arr = new int[mapIdx];
        int[] idx = new int[mapIdx];
        for (int i = 0; i < mapIdx; i++) {
            arr[i] = map.get(sorted.get(i));
            idx[map.get(sorted.get(i))] = i;
        }

        for (int i = 0; i < n; i++) {
            int first = map.get(a[i]), second = map.get(b[i]);
            int idx1 = idx[first], idx2 = idx[second];
            arr[idx1] = second;
            arr[idx2] = first;
            idx[first] = idx2;
            idx[second] = idx1;
        }
        int[] find = new int[arr.length];
        for (int i = 0; i < arr.length; i++)
            find[i] = rev[arr[i]];

        mergeSort(find, 0, mapIdx - 1);
        for (int i = 0; i < mapIdx; i++) {
            int originalNum = rev[arr[i]];
//            System.err.println(originalNum + " " + idx[i] + " " + sorted.get(idx[i]));
//            System.err.println(posInSorted.get(sorted.get(idx[i])));
//            System.err.println((posInSorted.get(originalNum) - posInSorted.get(sorted.get(idx[i])) - 1));
//            System.err.println((originalNum - sorted.get(idx[i]) - 1) - (posInSorted.get(originalNum) - posInSorted.get(sorted.get(idx[i])) - 1));
            res += Math.abs(originalNum - sorted.get(idx[arr[i]])) - 1 -
                    (Math.abs(posInSorted.get(originalNum) - posInSorted.get(sorted.get(idx[arr[i]]))) - 1);
        }

        out.println(res);
        out.flush();
        out.close();
    }

    static void mergeSort (int arr [] , int start , int end){

        if (start < end) {
            int mid = (start + end) >> 1;
            mergeSort(arr , start , mid);
            mergeSort(arr , mid + 1 , end);

            int size1 = mid - start + 1 ;
            int size2 = end - mid;

            int L [] = new int[size1 + 1];
            int R [] = new int[size2 + 1];

            for (int i = 0 ; i < size1 ; ++i) L[i] = arr[start + i]; L[size1] = (int)1e9;
            for (int i = 0 ; i < size2 ; ++i) R[i] = arr[mid + 1 + i]; R[size2] = (int)1e9;

            for (int i = 0 , j = 0 , idx = start ; idx <= end ; ++ idx)
                if (L[i] < R[j]) arr[idx] = L[i ++];
                else {
                    arr[idx] = R[j ++];
                    if (j - 1 != size2) res += (size1 - i);
                }

        }
    }






















    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException {
            return br.ready();
        }

    }
}