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.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class InfiniteInversions {
	static class Pair implements Comparable<Pair> {
		int index, value;

		public Pair(int i, int v) {
			index = i;
			value = v;
		}

		@Override
		public int compareTo(InfiniteInversions.Pair o) {
			return index - o.index;
		}

	}

	static long[] bit;

	static long getSum(int index) {
		long sum = 0;
		index = index + 1;
		while (index > 0) {
			sum += bit[index];
			index -= index & (-index);
		}
		return sum;
	}

	static void updateBIT(int index, long val) {
		index = index + 1;
		while (index < bit.length) {
			bit[index] += val;
			index += index & (-index);
		}
	}

	public static void main(String[] args) {
		InputReader r = new InputReader(System.in);
		int n = r.nextInt();
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(n);
		for (int i = 0; i < n; i++) {
			int from = r.nextInt();
			int to = r.nextInt();
			int fromNum = from;
			int toNum = to;
			if (map.containsKey(from))
				fromNum = map.get(from);
			if (map.containsKey(to))
				toNum = map.get(to);
			map.put(to, fromNum);
			map.put(from, toNum);
		}
		Pair[] arr = new Pair[map.size() * 3];
		int curr = 0;
		TreeSet<Integer> values = new TreeSet<Integer>();
		for (Entry<Integer, Integer> e : map.entrySet()) {
			int index = e.getKey();
			int value = e.getValue();
			arr[curr++] = new Pair(index, value);
			values.add(value);
			for (int k = -1; k <= 1; k += 2) {
				int newIndex = index + k;
				if (!map.containsKey(newIndex) && !values.contains(newIndex)) {
					int newValue = newIndex;
					arr[curr++] = new Pair(newIndex, newValue);
					values.add(newValue);

				}
			}
		}
		HashMap<Integer, Integer> revIndex = new HashMap<Integer, Integer>(values.size());
		int count = 1;
		for (int x : values)
			revIndex.put(x, count++);
		Arrays.sort(arr, 0, curr);
		long res = 0;
		bit = new long[count + 5];
		for (int i = curr - 1; i >= 0; i--) {
			long v = getSum(revIndex.get(arr[i].value));
			res += v;
			if (i < curr - 1) {
				int currIndex = arr[i].index;
				int nextIndex = arr[i + 1].index;
				long len = nextIndex - currIndex - 1;
				if (len > 0) {
					res += len * v;
					updateBIT(revIndex.get(arr[i].value), len + 1);
				} else {
					updateBIT(revIndex.get(arr[i].value), 1);
				}
			} else {
				updateBIT(revIndex.get(arr[i].value), 1);
			}
		}
		System.out.println(res);
	}

	static class InputReader {
		private BufferedReader reader;
		private StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream));
			tokenizer = null;
		}

		public InputReader(FileReader stream) {
			reader = new BufferedReader(stream);
			tokenizer = null;
		}

		public String nextLine() {
			try {
				return reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return 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());
		}

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

		public double nextDouble() {
			return Double.parseDouble(next());
		}
	}
}
