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.Reader;
import java.io.FileInputStream;
import java.util.TreeMap;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Map;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Set;
import java.util.StringTokenizer;

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

class TaskE {
    public void solve(int testNumber, FastReader in, PrintWriter out) {
        int n = in.nextInt();
        Map<Integer, Integer> numberOnPos = new TreeMap<>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            Integer x = numberOnPos.get(a);
            if (x == null) {
                x = a;
            }
            Integer y = numberOnPos.get(b);
            if (y == null) {
                y = b;
            }
            numberOnPos.put(a, y);
            numberOnPos.put(b, x);
        }
        int[] position = new int[numberOnPos.size()];
        int[] number = new int[numberOnPos.size()];
        int size = 0;
        for (Integer x : numberOnPos.keySet()) {
            position[size] = x;
            number[size++] = numberOnPos.get(x);
        }
        long ans = getInversions(number.clone(), 0, number.length);
        for (int i = 0; i < size; i++) {
            int a = number[i];
            int b = position[i];
            if (a > b) {
                int t = a;
                a = b;
                b = t;
            }
            int l = getPos(position, a);
            int r = getPos(position, b);
            int count = r - l - 1;
            count = b - a - 1 - count;
            System.err.println(a + " " + b + " : " + count);
            ans += count;
        }
        out.println(ans);
    }

    int getPos(int[] a, int x) {
        int l = -1;
        int r = a.length;
        while (r - l > 1) {
            int m = (l + r) / 2;
            if (a[m] <= x) {
                l = m;
            } else {
                r = m;
            }
        }
        return l;
    }

    long getInversions(int[] a, int l, int r) {
        if (r - l <= 1) {
            return 0L;
        }
        int m = (l + r) / 2;
        long res = getInversions(a, l, m) + getInversions(a, m, r);
        int[] buf = new int[r - l];
        int size = 0;
        for (int i = l, j = m; i < m || j < r;) {
            if (i < m && (j == r || a[i] <= a[j])) {
                buf[size++] = a[i];
                i++;
            } else {
                buf[size++] = a[j];
                j++;
                res += m - i;
            }
        }
        for (int i = 0; i < size; i++) {
            a[i + l] = buf[i];
        }
        return res;
    }
}

class FastReader extends BufferedReader {
    private StringTokenizer st;

    public FastReader(InputStream is) {
        super(new InputStreamReader(is));
    }

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

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

}

