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.TreeSet;
import java.util.InputMismatchException;
import java.io.InputStream;
import java.util.HashMap;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author George Marcus
 */
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 {
    int[] aib;

    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int N = in.nextInt();
        int[][] A = new int[N][2];

        TreeSet<Integer> set = new TreeSet<Integer>();
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < N; i++) {
            A[i][0] = in.nextInt();
            A[i][1] = in.nextInt();
            set.add(A[i][0]);
            set.add(A[i][1]);
        }

        int[] B = new int[2 * N];
        int[] C = new int[2 * N];
        int K = 0;
        for (int x : set) {
            B[K] = x;
            C[K] = x;
            map.put(x, K++);
        }

        for (int i = 0; i < N; i++) {
            int a = map.get(A[i][0]);
            int b = map.get(A[i][1]);
            int aux = B[a];
            B[a] = B[b];
            B[b] = aux;
        }

        HashMap<Integer, Integer> newPos = new HashMap<Integer, Integer>();
        for (int i = 0; i < K; i++) {
            int x = B[i];
            newPos.put(x, i);
        }

        aib = new int[K + 1];
        int total = 0;
        long ans = 0;
        for (int x : set) {
            int pos = newPos.get(x);
            int q = total - query(pos);
            ans += q;
            update(pos, 1);
            total++;
        }

        for (int i = 0; i < K; i++) {
            int x = B[i];
            int pos1 = map.get(x);
            int pos2 = i;
//            if (newPos.get(x) != i) {
//                throw new RuntimeException();
//            }
            if (pos1 > pos2) {
                int aux = pos1;
                pos1 = pos2;
                pos2 = aux;
            }
            if (pos1 != pos2) {
                int num = pos2 - pos1;
                int range = C[pos2] - C[pos1];
                ans += range - num;
            }
        }

        out.println(ans);
    }

    private void update(int pos, int val) {
        pos++;
        while (pos < aib.length) {
            aib[pos] += val;
            pos += pos & (-pos);
        }
    }

    private int query(int pos) {
        pos++;
        int ret = 0;
        while (pos > 0) {
            ret += aib[pos];
            pos -= pos & (-pos);
        }
        return ret;
    }
}

class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public InputReader(InputStream stream) {
        this.stream = stream;
    }

    public int read() {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars) {
            curChar = 0;
            try {
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

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

    public String nextString() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuffer res = new StringBuffer();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));

        return res.toString();
    }

    private boolean isSpaceChar(int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

}

