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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author K0T
 */
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 {

    static final int C = 200500;

    int n;
    int border;
    int[] diff;
    int[] a, b;
    int[] pa, pb;
    int[] fw;

    public void solve(int testNumber, InputReader scn, PrintWriter pw) {
        n = scn.nextInt();
        a = new int[n + C];
        b = new int[n + C];
        fw = new int[n + C];
        int sz = 0;
        diff = new int[n + n + C];
        for (int i = 1; i <= n; i++) {
            a[i] = scn.nextInt();
            b[i] = scn.nextInt();
            diff[++sz] = a[i];
            diff[++sz] = b[i];
        }
        Arrays.sort(diff, 1, sz + 1);
        int[] temp = new int[n + n + C];
        temp[1] = diff[1];
        int all = 1;
        for (int i = 2; i <= sz; i++) {
            if (diff[i] != temp[all]) {
                temp[++all] = diff[i];
            }
        }
        sz = all;
        for (int i = 1; i <= sz; i++) {
            diff[i] = temp[i];
        }
        int[] p = new int[n + C];
        for (int i = 1; i <= sz; i++) {
            p[i] = i;
        }
        pa = new int[n + C];
        pb = new int[n + C];
        border = sz;
        Arrays.fill(fw, 0);
        for (int i = 1; i <= n; i++) {
            pa[i] = getIdx(a[i]);
            pb[i] = getIdx(b[i]);
            int c = p[pa[i]];
            p[pa[i]] = p[pb[i]];
            p[pb[i]] = c;
        }
        long result = 0L;
        for (int i = 1; i <= sz; i++) {
            result += (1L * getSum(border));
            result -= (1L * getSum(p[i]));
            update(p[i], 1);
        }
        int[] positions = new int[n + C];
        for (int i = 1; i <= sz; i++) {
            positions[p[i]] = i;
        }
        for (int i = 1; i <= sz; i++) {
            int whoNow = diff[p[i]];
            int whoWas = diff[i];
            int delta = Math.abs(whoNow - whoWas) - 1;
            int other = getIdx(diff[p[i]]);
            int d_i = Math.abs(other - i) - 1;
            result += (1L * delta);
            result -= (1L * d_i);
        }
        pw.println(result);
    }

    public int getIdx(int x) {
        int le = 1;
        int ri = border + 1;
        while (ri - le > 1) {
            int mid = (le + ri) >> 1;
            if (diff[mid] <= x) {
                le = mid;
            } else {
                ri = mid;
            }
        }
        return le;
    }

    public void update(int pos, int add) {
        while (pos <= border) {
            fw[pos] += add;
            pos |= (pos + 1);
        }
    }

    public int getSum(int r) {
        int res = 0;
        while (r > 0) {
            res += fw[r];
            r = (r & (r + 1)) - 1;
        }
        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());
    }

}
