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

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

class TaskE {

    public void solve(int testNumber, FastScanner in, PrintWriter out) {
        int n = in.nextInt();
        TreeSet<Integer> was = new TreeSet<>();
        int[] qa = new int[n];
        int[] qb = new int[n];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            qa[i] = a;
            qb[i] = b;
            was.add(a);
            was.add(b);
        }
        ArrayList<Integer> a = new ArrayList<>();
        for (int x : was)
            a.add(x);
        ArrayList<Pair> all = new ArrayList<>();
        ArrayList<Integer> unique = new ArrayList<>();
        for (int i = 0; i < a.size() - 1; i++) {
            all.add(new Pair(a.get(i), 1));
            unique.add(a.get(i));
            if (a.get(i) != a.get(i + 1) - 1) {
                unique.add(a.get(i) + 1);
                all.add(new Pair(a.get(i) + 1, a.get(i + 1) - a.get(i) - 1));
            }
        }
        unique.add(a.get(a.size() - 1));
        all.add(new Pair(a.get(a.size() - 1), 1));
        int[] where = new int[unique.size()];
        for (int i = 0; i < where.length; i++)
            where[i] = i;
        for (int i = 0; i < n; i++) {
            int valA = Collections.binarySearch(unique, qa[i]);
            int valB = Collections.binarySearch(unique, qb[i]);
            int t = where[valA];
            where[valA] = where[valB];
            where[valB] = t;
            Collections.swap(all, where[valA], where[valB]);
        }
        Bit tree = new Bit(unique.size() + 1);
        long res = 0;
        for (Pair p : all) {
            int val = p.first;
            long sz = p.second;
            val = Collections.binarySearch(unique, val);
            res += tree.get(val + 1, unique.size()) * sz;
            tree.upd(val, sz);
        }
        out.println(res);
    }

    class Bit {
        long[] f;

        Bit(int size) {
            f = new long[size];
        }

        void upd(int at, long delta) {
            for (; at < f.length; at = (at | (at + 1)))
                f[at] += delta;
        }

        long get(int at) {
            long res = 0;
            for (; at >= 0; at = (at & (at + 1)) - 1)
                res += f[at];
            return res;
        }

        long get(int l, int r) {
            return get(r) - get(l - 1);
        }
    }
}

class FastScanner {
    private StringTokenizer tokenizer;
    private BufferedReader reader;

    public FastScanner(InputStream inputStream) {
        reader = new BufferedReader(new InputStreamReader(inputStream));
    }

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

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

}

class Pair implements Comparable<Pair> {
    public int first;
    public int second;

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    public int compareTo(Pair p) {
        if (first != p.first)
            return Integer.compare(first, p.first);
        return Integer.compare(second, p.second);
    }

    public int hashCode() {
        return first * 1_000_003 + second;
    }

    public boolean equals(Object o) {
        if (o == null || !(o instanceof Pair))
            return false;
        Pair p = (Pair) o;
        return first == p.first && second == p.second;
    }
}

