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

/**
 * @author Don Li
 */
public class InfiniteInversions {
    void solve() {
        int n = in.nextInt();

        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt(), b = in.nextInt();
            int x = !map.containsKey(a) ? a : map.get(a);
            int y = !map.containsKey(b) ? b : map.get(b);
            map.put(a, y);
            map.put(b, x);
        }

        Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, Integer> entry = it.next();
            if (entry.getKey().equals(entry.getValue())) it.remove();
        }

        int p = 0, m = map.size();
        int[] pos = new int[m], val = new int[m];
        for (int i : map.keySet()) {
            pos[p] = i;
            val[p] = map.get(i);
            p++;
        }
        Arrays.sort(pos);

        long ans = count(val);

        for (int i : map.keySet()) {
            int x = map.get(i);

            if (i < x) {
                int a = lowerBound(pos, i + 1), b = upperBound(pos, x);
                ans += Math.max(0, x - i - 1) - (b - a - 1);
            }

            if (x < i) {
                int a = lowerBound(pos, x + 1), b = upperBound(pos, i);
                ans += Math.max(0, i - x - 1) - (b - a - 1);
            }
        }

        out.println(ans);
    }

    long count(int[] A) {
        return count(A, 0, A.length);
    }

    long count(int[] A, int p, int r) {
        if (r - p <= 1) return 0;

        long res = 0;
        int q = (p + r) / 2;
        res += count(A, p, q);
        res += count(A, q, r);

        int m = q - p, n = r - q;
        int[] B = new int[m], C = new int[n];
        System.arraycopy(A, p, B, 0, m);
        System.arraycopy(A, q, C, 0, n);
        int i = 0, j = 0, k = p;
        while (i < m || j < n) {
            if (i == m || j < n && B[i] > C[j]) {
                res += m - i;
                A[k++] = C[j++];
            } else {
                A[k++] = B[i++];
            }
        }

        return res;
    }

    int lowerBound(int[] A, int x) {
        int lb = -1, ub = A.length;
        while (ub - lb > 1) {
            int m = (lb + ub) / 2;
            if (A[m] >= x) ub = m;
            else lb = m;
        }
        return ub;
    }

    int upperBound(int[] A, int x) {
        int lb = -1, ub = A.length;
        while (ub - lb > 1) {
            int m = (lb + ub) / 2;
            if (A[m] > x) ub = m;
            else lb = m;
        }
        return ub;
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new InfiniteInversions().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        public long nextLong() {
            return Long.parseLong(nextToken());
        }

        public double nextDouble() {
            return Double.parseDouble(nextToken());
        }
    }
}
