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.util.Arrays;
import java.util.HashSet;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }
}

class TaskE {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int numSwaps = in.nextInt();
        HashMap<Integer, Integer> sequence = new HashMap<Integer, Integer>();
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < numSwaps; i++) {
            int firstIndex = in.nextInt();
            int secondIndex = in.nextInt();
            if (!set.contains(firstIndex)) set.add(firstIndex);
            if (!set.contains(secondIndex)) set.add(secondIndex);
            int firstValue, secondValue;
            firstValue = firstIndex;
            if (sequence.containsKey(firstIndex)) {
                firstValue = sequence.get(firstIndex);
            }
            secondValue = secondIndex;
            if (sequence.containsKey(secondIndex)) {
                secondValue = sequence.get(secondIndex);
            }
            sequence.put(firstIndex, secondValue);
            sequence.put(secondIndex, firstValue);
        }
        Integer[] a = new Integer[set.size()];
        Iterator it = set.iterator();
        for (int i = 0; i < set.size(); i++) {
            a[i] = (Integer) it.next();
        }
        Arrays.sort(a);
        HashMap<Integer, Integer> order = new HashMap<Integer, Integer>();
        for (int i = 0; i < a.length; i++) {
            order.put(a[i], i);
        }
        FenwickTree fenwickTree = new FenwickTree(a.length);
        long res = 0;
        for (int i = a.length - 1; i >= 0; i--) {
            int rank = order.get(sequence.get(a[i]));
            res += fenwickTree.sum(rank - 1);
            fenwickTree.add(rank, 1);
        }
        for (int i = 0; i < a.length; i++) {
            int value = sequence.get(a[i]);
            int rankA = order.get(a[i]);
            int rankB = order.get(value);
            res += Math.max(0, Math.max(0, Math.abs(a[i] - value) - 1) - Math.max(0, Math.abs(rankA - rankB) - 1));
        }
        out.println(res);
    }
}

class FenwickTree {
    int[] t;

    public FenwickTree(int n) {
        t = new int[n];
    }

    public void add(int i, int value) {
        for (; i < t.length; i |= i + 1)
            t[i] += value;
    }

    public int sum(int i) {
        int res = 0;
        for (; i >= 0; i = (i & (i + 1)) - 1)
            res += t[i];
        return res;
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream), 32768);
        tokenizer = null;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

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

}
