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.*;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public
class E
{
    public static
    void main(String[] args) throws IOException
    {
        new E().run();
    }

    class FenwickTree
    {
        int n;
        int[] a;

        public
        FenwickTree(int n)
        {
            this.n = n;
            a = new int[this.n];
        }

        private
        int sum(int r)
        {
            int res = 0;
            for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
            {
                res += a[i];
            }

            return res;
        }

        private
        int sum(int l, int r)
        {
            return sum(r) - sum(l - 1);
        }

        private
        void inc(int i)
        {
            for (; i < n; i |= i + 1)
            {
                ++a[i];
            }
        }
    }

    class Item
            implements Comparable<Item>
    {
        int index;
        int value;

        public
        Item(int index, int value)
        {
            this.index = index;
            this.value = value;
        }

        @Override
        public
        int compareTo(Item item)
        {
            if (index == item.index)
            {
                return Integer.compare(value, item.value);
            }
            return Integer.compare(index, item.index);
        }
    }


    private
    void condense(int[] a)
    {
        TreeSet<Integer> set = new TreeSet<>();
        for (int foo : a)
        {
            set.add(foo);
        }

        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int foo : set)
        {
            map.put(foo, map.size());
        }
        for (int i = 0; i < a.length; ++i)
        {
            a[i] = map.get(a[i]);
        }
    }


    private
    void run() throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        int q = Integer.parseInt(in.readLine());

        TreeMap<Integer, Integer> map = new TreeMap<>();

        for (int i = 0; i < q; ++i)
        {
            String[] tokens = in.readLine().split(" ");
            int left = Integer.parseInt(tokens[0]);
            int right = Integer.parseInt(tokens[1]);

            int number_left = map.getOrDefault(left, left);
            int number_right = map.getOrDefault(right, right);

            map.put(left, number_right);
            map.put(right, number_left);
        }

        Item[] items = new Item[map.size()];

        int n = 0;

        for (Map.Entry<Integer, Integer> entry : map.entrySet())
        {
            items[n++] = new Item(entry.getKey(), entry.getValue());
        }

        int[] a = new int[n];

        for (int i = 0; i < n; ++i)
        {
            a[i] = items[i].value;
        }

        condense(a);

        long ans = 0;

        FenwickTree fenwickTree = new FenwickTree(n);
        for (int i = 0; i < n; ++i)
        {
            ans += fenwickTree.sum(a[i], n - 1);
            fenwickTree.inc(a[i]);
        }

        for (int right = 0; right < n; ++right)
        {
            int left = ~Arrays.binarySearch(items, new Item(items[right].value, -1));
            ans += Math.abs(items[right].index - items[left].index);
            ans -= Math.abs(right - left);
        }

        System.out.println(ans);
    }
}
