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

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

class TaskE {

    class Pair {
        public int x, y, z;
        public Pair(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public String toString() {
            return x + " " + y + " " + z;
        }
    }
    ArrayList<Pair> usedPos;
    public void solve(int testNumber, Reader in, PrintWriter out) {
        int n = in.ni();
        HashMap<Integer, Integer> pos2val = new HashMap<>();
        int a, b;
        int toReplaceA, toReplaceB;
        for(int i = 0; i < n; i++) {
            a = in.ni();
            b = in.ni();
            toReplaceA = pos2val.containsKey(a) ? pos2val.get(a) : a;
            toReplaceB = pos2val.containsKey(b) ? pos2val.get(b) : b;
            pos2val.put(b, toReplaceA);
            pos2val.put(a, toReplaceB);
        }
        ArrayList<Integer> pos2remove = new ArrayList<>();
        for(Integer key : pos2val.keySet()) {
            if(pos2val.get(key) == key) {
                pos2remove.add(key);
            }
        }
        for(int i = 0; i < pos2remove.size(); i++) {
            pos2val.remove(pos2remove.get(i));
        }

        usedPos = new ArrayList<>();
        for(Integer key : pos2val.keySet()) {
            usedPos.add(new Pair(key, pos2val.get(key), 0));
        }
        int m = usedPos.size();
        Collections.sort(usedPos, new Comparator<Pair>() {
            public int compare(Pair o1, Pair o2) {
                if(o1.x < o2.x) {
                    return -1;
                }
                if(o1.x > o2.x) {
                    return 1;
                }
                return 0;
            }
        });
        ArrayList<Pair> tempPairs = new ArrayList<>();
        for(int i = 0; i < m; i++) {
            tempPairs.add(usedPos.get(i));
        }
        Collections.sort(tempPairs, new Comparator<Pair>() {
            public int compare(Pair o1, Pair o2) {
                if(o1.y < o2.y) {
                    return -1;
                }
                if(o1.y > o2.y) {
                    return 1;
                }
                return 0;
            }
        });
        for(int i = 0; i < m; i++) {
            tempPairs.get(i).z = i;
        }
        FenwickTree ft = new FenwickTree(m);
        long ans = 0;
        for(int i = m - 1; i >= 0; i--) {
            ans += ft.sum(usedPos.get(i).z);
            ft.inc(usedPos.get(i).z, 1);
        }
        for(int i = 0; i < m; i++) {
            Pair pair = usedPos.get(i);
            if(pair.x < pair.y) {
                ans += pair.y - pair.x - bs(pair.x, pair.y);
            } else {
                ans += pair.x - pair.y - bs(pair.y, pair.x);
            }
        }
        out.println(ans);

    }

    public int bs(int l, int r) {
        int m = usedPos.size();
        int left = 0;
        int right = m - 1;
        int mid;
        while(left < right) {
            mid = left + (right - left) / 2;
            if(usedPos.get(mid).x < l) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        int leftPos = left;
        left = 0;
        right = m - 1;
        while(left < right) {
            mid = left + (right - left) / 2;
            if(usedPos.get(mid).x <= r) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if(usedPos.get(left).x > r) {
            left--;
        }
        return left - leftPos;
    }
}

class Reader {
    private BufferedReader in;
    private StringTokenizer st = new StringTokenizer("");
    private String delim = " ";

    public Reader(InputStream in) {
        this.in = new BufferedReader(new InputStreamReader(in));
    }

    public String next() {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(rl());
        }
        return st.nextToken(delim);
    }

    public String rl() {
        try {
            return in.readLine();
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }

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

}

class FenwickTree {
    int n, b[];

    public FenwickTree(int n) {
        this.n = n;
        b = new int[n];
    }

    public void inc(int i, int delta) {
        for(; i < n; i = (i | (i + 1))) {
            b[i] += delta;
        }
    }

    public int sum(int r) {
        int result = 0;
        for(; r >= 0; r = (r & (r + 1)) - 1) {
            result += b[r];
        }
        return result;
    }

}

