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 javax.swing.text.Segment;
import java.io.*;
import static java.lang.Math.*;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.*;
import java.lang.*;

public class Main {
    final static boolean debug = false;
    final static String fileName = "";
    final static boolean useFiles = false;

    public static void main(String[] args) throws FileNotFoundException {
        long start;
        if (debug)
            start = System.nanoTime();
        InputStream inputStream;
        OutputStream outputStream;
        if (useFiles) {
            inputStream = new FileInputStream(fileName + ".in");
            outputStream = new FileOutputStream(fileName + ".out");
        } else {
            inputStream = System.in;
            outputStream = System.out;
        }
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task(in, out);
        solver.solve();
        if(debug)
            out.println((System.nanoTime() - start) / 1e+9);
        out.close();
    }
}

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 double nextDouble(){
        return Double.parseDouble(next());
    }

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

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

    public byte nextByte(){
        return Byte.parseByte(next());
    }
}

class Element implements Comparable<Element>{
    int Value, Count;

    Element(int value, int count) {
        Value = value;
        Count = count;
    }

    @Override
    public int compareTo(Element o) {
        return Value - o.Value;
    }
}

class Task {
    ArrayList<Integer> compress(ArrayList<Integer> all) {
        Collections.sort(all);
        ArrayList<Integer> result = new ArrayList<>();
        result.add(all.get(0));
        for (int i = 1; i < all.size(); i++) {
            if (!Objects.equals(all.get(i - 1), all.get(i)))
                result.add(all.get(i));
        }
        return result;
    }
    public void solve() {
        int swapCount = in.nextInt();
        int[] a = new int[swapCount];
        int[] b = new int[swapCount];
        ArrayList<Integer> all = new ArrayList<>();
        for (int i = 0; i < swapCount; i++) {
            a[i] = in.nextInt();
            b[i] = in.nextInt();
            all.add(a[i]);
            all.add(b[i]);
        }
        all = compress(all);
        ArrayList<Element> x = new ArrayList<>();
        for (int i = 0; i < all.size() - 1; i++) {
            x.add(new Element(all.get(i), 1));
            if (all.get(i + 1) - all.get(i) >= 2)
                x.add(new Element(all.get(i) + 1, all.get(i + 1) - all.get(i) - 1));
        }
        x.add(new Element(all.get(all.size() - 1), 1));

        for (int i = 0; i < swapCount; i++) {
            a[i] = Collections.binarySearch(x, new Element(a[i], 0));
            b[i] = Collections.binarySearch(x, new Element(b[i], 0));
        }
        for (int i = 0; i < x.size(); i++) {
            x.get(i).Value = i;
        }

        for (int i = 0; i < swapCount; i++) {
            Element t = x.get(a[i]);
            x.set(a[i], x.get(b[i]));
            x.set(b[i], t);
        }

//        for (int i = 0; i < x.size(); i++){
//            out.println(x.get(i).Value + " " + x.get(i).Count);
//        }

        int n = x.size();
        SumInIntervalAddToElementSegmentTree tree = new SumInIntervalAddToElementSegmentTree(n);
        long inversionsCount = 0;
        for (int i = n - 1; i >= 0; i--) {
            Element element = x.get(i);
            if (element.Value > 0) {
                inversionsCount += element.Count * (long) tree.get(0, element.Value - 1);
//                out.println(element.Count * (long) tree.get(0, element.Value - 1));
//                out.println(tree.toString());
            }
            tree.add(element.Value, element.Count);
        }
        out.println(inversionsCount);
    }

    private InputReader in;
    private PrintWriter out;

    Task(InputReader in, PrintWriter out) {
        this.in = in;
        this.out = out;
    }
}

class SumInIntervalAddToElementSegmentTree {
    private int[] t;
    private int n;

    SumInIntervalAddToElementSegmentTree(int n) {
        this.n = n;
        t = new int[Integer.highestOneBit(n) << 2];
    }

    SumInIntervalAddToElementSegmentTree(int[] array) {
        this(array.length);
        build(0, 0, n - 1, array);
    }

    private void build(int v, int tl, int tr, int[] array) {
        if (tr == tl) {
            t[v] = array[tl];
        } else {
            int tm = (tl + tr) >> 1;
            build(2 * v + 1, tl, tm, array);
            build(2 * v + 2, tm + 1, tr, array);
            t[v] = t[2 * v + 1] + t[2 * v + 2];
        }
    }

    int get(int l, int r) {
        if (l > n || r > n || 0 > l || 0 > r)
            throw new IllegalArgumentException("indices: (" + l + ", " + r + ") . Bounds: (0, "+ (n - 1));
        return get(0, 0, n - 1, l, r);
    }

    void add(int index, int value) {
        if (0 > index || index > n)
            throw new IllegalArgumentException("index: " + index + ". Bounds: " + (n - 1));
        set(0, 0, n - 1, index, value);
    }

    int search(int value) {
        return search(0, 0, n - 1, value);
    }

    private int search(int v, int tl, int tr, int value) {
        if (tl == tr)
            return tr;

        int tm = (tl + tr) / 2;
        if (value <= t[2 * v + 1])
            return search(2 * v + 1, tl, tm, value);
        return search(2 * v + 2, tm + 1, tr, value - t[2 * v + 1]);
    }

    private int get(int v, int tl, int tr, int l, int r) {
        if (l > r)
            return 0;
        if (tl == l && tr == r)
            return t[v];
        int tm = (tl + tr) >> 1;
        return get(2 * v + 1, tl, tm, l, Math.min(tm, r)) +
                get(2 * v + 2, tm + 1, tr, Math.max(tm + 1, l), r);
    }

    private void set(int v, int tl, int tr, int index, int value) {
        if (tr == tl)
            t[v] += value;
        else {
            int tm = (tl + tr) >> 1;
            if (index <= tm)
                set(2 * v + 1, tl, tm, index, value);
            else
                set(2 * v + 2, tm + 1, tr, index, value);
            t[v] = t[2 * v + 1] + t[2 * v + 2];
        }
    }

    public int[] toArray() {
        int[] result = new int[n];
        for (int i = 0; i < n; i++)
            result[i] = get(i, i);
        return result;
    }

    @Override
    public String toString() {
        String result = "[";
        for (int i = 0; i < n; i++)
            result += get(i, i) + ",";
        return result + "]";
    }
}