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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.TreeMap;
import java.util.Set;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

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

class TaskE {
    public void solve(int testNumber, FastScanner in, FastPrinter out) {
        int n = in.nextInt();
        TreeMap<Integer,Integer> pos = new TreeMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            int b = in.nextInt();
            int aval = pos.containsKey(a)?pos.get(a):a;
            int bval = pos.containsKey(b)?pos.get(b):b;
            pos.put(a, bval);
            pos.put(b, aval);
        }
        int [] vals = new int[pos.size()];
        int [] vals2 = new int[pos.size()];
        int i=0;
        TreeMap<Integer,Integer> apos = new TreeMap<Integer, Integer>();
        TreeMap<Integer,Integer> pos2 = new TreeMap<Integer, Integer>();
        for(int k:pos.keySet()){
            int v = pos.get(k);
            vals[i] = v;
            vals2[i] = v;
            pos2.put(v, i);
            apos.put(v, k);
            i++;
        }
        Arrays.sort(vals);
        int size = 1;
        while (size<i) size<<=1;
        int [] segmentTree = new int[size*2];
        Arrays.fill(segmentTree,0);
        long ans = 0;
        for (int v : vals) {
            int j1 = pos2.get(v);
            int j2 = pos2.get(pos.get(v));
            ans += get(segmentTree, 1, j1, i, 0, size);
            ans += Math.abs(apos.get(vals2[j1]) - apos.get(vals2[j2]) - j1 + j2);
            set(segmentTree,size+j1,1);
        }
        out.printLine(ans);

    }

    int get(int[]tree,int i, int l, int r, int L, int R){
        if (l<=L && r>=R){
            return tree[i];
        }
        if (l>=R || r<=L){
            return 0;
        }
        int M = L+(R-L)/2;
        return get(tree, i*2, l, r, L, M)+get(tree, i*2+1, l, r, M, R);
    }
    void set(int[]tree, int i, int val){
        tree[i] = val;
        i/=2;
        while (i>0){
            tree[i] = tree[2*i]+tree[2*i+1];
            i/=2;
        }
    }
}

class FastScanner {

    private BufferedReader reader;
    private StringTokenizer st;

    public FastScanner(InputStream stream) {
        this.reader = new BufferedReader(new InputStreamReader(stream));
        this.st = new StringTokenizer("");
    }

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

    public String next(){
        while (!st.hasMoreTokens()){
            st = new StringTokenizer(readLine());
        }
        return st.nextToken();
    }

    private String readLine() {
        try {
            String line = reader.readLine();
            if (line==null) throw new InputMismatchException();
            return line;
        } catch (IOException e) {
            throw new InputMismatchException();
        }
    }

}

class FastPrinter {
    private final PrintWriter writer;

    public FastPrinter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public void close() {
        writer.close();
    }

    public void printLine(long i) {
        writer.println(i);
    }

}
