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.InputMismatchException;
import java.util.ArrayDeque;
import java.util.List;
import java.util.NoSuchElementException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Queue;
import java.io.IOException;
import java.util.TreeSet;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.PrintStream;
import java.util.StringTokenizer;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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 {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        long ans = 0;
        int n = in.nextInt();
        TreeSet<Integer> treeSet = new TreeSet<Integer>();
        HashMap<Integer,Integer> numberToIndex = new HashMap<Integer, Integer>();
        HashMap<Integer,Integer> treeMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            if (treeMap.containsKey(x) && treeMap.containsKey(y)){
                int d = treeMap.get(y);
                treeMap.put(y,treeMap.get(x));
                treeMap.put(x,d);
            }
            else if (treeMap.containsKey(x)){
                int d = treeMap.get(x);
                treeMap.put(x,y);
                treeMap.put(y,d);
            }
            else if (treeMap.containsKey(y)){
                int d = treeMap.get(y);
                treeMap.put(y,x);
                treeMap.put(x,d);
            }
            else {
                treeMap.put(x,y);
                treeMap.put(y,x);
            }
            numberToIndex.put(treeMap.get(x),x);
            numberToIndex.put(treeMap.get(y),y);
            treeSet.add(treeMap.get(x));
            treeSet.add(treeMap.get(y));
        }
        RBTree<Integer,Boolean> set2 = new RBTree<Integer, Boolean>();
        for(int g:treeSet){
            ans += set2.size(numberToIndex.get(g), (int)1e9 + 4);
            set2.put(numberToIndex.get(g), true);
        }
        for(int g:treeSet){
            int ind = numberToIndex.get(g);
            if (ind < g){
                int l = set2.size(ind + 1, g);
                int k = (g - ind - l);
                ans += k;
            }
            else {
                int l = set2.size(g, ind - 1);
                int k = (ind - g - l);
                ans += k;
            }
        }
        out.print(ans);
    }
}

class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
        reader = new BufferedReader(new InputStreamReader(stream));
        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());
    }


}

class RBTree<Key extends Comparable<Key>,Value> {
    private static final boolean RED = true;
    private static final boolean BLACK = false;
    Node root;
    private class Node{
        private int count;
        private Key key;
        private Value value;
        private boolean color;
        private Node left, right;
        public Node(Key key, Value value, boolean color) {
            this.key = key;
            this.value = value;
            this.color = color;
            count = 1;
        }
    }

    public Value getValue(Key key){
        return getValue(root, key);
    }

    private Value getValue(Node root, Key key) {
        Node g = root;
        while (g != null){
            int cmp = g.key.compareTo(key);
            if (cmp < 0)g = g.right;
            else if (cmp > 0)g = g.left;
            else return g.value;
        }
        return null;
    }

    public boolean contains(Key key){
        return getValue(key) != null;
    }

    public void put(Key key, Value value){
        root = put(root, key, value);
        root.color = BLACK;
    }

    private Node put(Node x, Key key, Value value) {
        if (x == null)return new Node(key, value, RED);
        int cmp = x.key.compareTo(key);
        if (cmp > 0)x.left = put(x.left, key, value);
        else if (cmp < 0)x.right = put(x.right, key, value);
        else x.value = value;

        if (isRed(x.right) && !isRed(x.left)) x = rotateLeft(x);
        if (isRed(x.left) && isRed(x.left.left)) x = rotateRight(x);
        if (isRed(x.left) && isRed(x.right)) flipColors(x);

        x.count = 1 + size(x.left) + size(x.right);
        return x;
    }

    private boolean isRed(Node x){
        if (x == null)return false;
        return x.color == RED;
    }

    private Node rotateLeft(Node x){
        Node t = x.right;
        x.right = t.left;
        t.left = x;
        t.color = x.color;
        x.color = RED;
        t.count = x.count;
        x.count = 1 + size(x.left) + size(x.right);
        return t;
    }

    private Node rotateRight(Node x){
        Node t = x.left;
        x.left = t.right;
        t.right = x;
        t.color = x.color;
        x.color = RED;
        t.count = x.count;
        x.count = 1 + size(x.left) + size(x.right);
        return t;
    }

    private void flipColors(Node x){
        x.color = !x.color;
        x.left.color = !x.left.color;
        x.right.color = !x.right.color;
    }

    public int rank(Key key) {
        return rank(key, root);
    }

    private int rank(Key key, Node x) {
        if (x == null) return 0;
        int cmp = key.compareTo(x.key);
        if      (cmp < 0) return rank(key, x.left);
        else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right);
        else
            return size(x.left);
    }

    public int size(Key lo, Key hi) {
        if (lo.compareTo(hi) > 0) return 0;
        if (contains(hi))
            return rank(hi) - rank(lo) + 1;
        else
            return rank(hi) - rank(lo);
    }

    private int size(Node root) {
        if (root == null)return 0;
        return root.count;
    }

}

