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.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class Main {

	static long count = 0;

	static int[] d;

	public static void main(String[] args) throws FileNotFoundException {
		InputReader inputReader = new InputReader(System.in);
		int n = inputReader.nextInt();
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		Set<Integer> set = new TreeSet<Integer>();
		for (int i = 0; i < n; i++) {
			int a = inputReader.nextInt();
			int b = inputReader.nextInt();

			Integer v1 = map.get(b);
			Integer v2 = map.get(a);

			if (v1 != null) {
				map.put(a, v1);
			}
			else {
				map.put(a, b);
			}

			if (v2 != null) {
				map.put(b, v2);
			}
			else {
				map.put(b, a);
			}
			set.add(a);
			set.add(b);
		}

		int k = 0;
		List<Integer> cList = new ArrayList<Integer>();
		for (int i : set) {
			if (map.get(i) != i) {
				cList.add(map.get(i));
			}
		}
		
		k = 0;
		int c[] = new int[cList.size()];
		for(int cValue : cList) {
			c[k++] = cValue;
		}
		
		d = Arrays.copyOf(c, c.length);

		mergeSort(d, 0, d.length-1);

		HashMap<Integer, Integer> pp = new HashMap<Integer, Integer>();
		for (int i = 0; i < c.length; i++) {
			pp.put(c[i], i);
		}

		for (int i = 0; i < c.length; i++) {
			int p = c[i];
			int v = map.get(p);
			count = count + (Math.abs(p - v) - 1);

			count = count - (Math.abs(pp.get(v) - pp.get(map.get(v))) - 1);
		}

		System.out.println(count);
	}

	public static void mergeSort(int c[], int start, int end) {
		if (start >= end) {
			return;
		}

		int mid = (start + end) / 2;

		mergeSort(c, start, mid);
		mergeSort(c, mid + 1, end);

		merge(c, start, mid, end);
	}

	public static void merge(int[] c, int start, int mid, int end) {
		int k = 0;
		int[] worker = new int[end - start + 1];
		int i = start, j = mid + 1;
		while (i <= mid && j <= end) {
			if (c[i] > c[j]) {
				count = count + (mid - i + 1);
				worker[k++] = c[j];
				j++;
			}
			else {
				worker[k++] = c[i];
				i++;
			}
		}

		while (i <= mid) {
			worker[k++] = c[i];
			i++;
		}

		while (j <= end) {
			worker[k++] = c[j];
			j++;
		}

		k = 0;
		for (i = start; i <= end; i++) {
			c[i] = worker[k++];
		}

	}
}

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 int nextInt() {
		return Integer.parseInt(next());
	}

}
