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.*;
import java.math.*;
import java.security.KeyStore.Entry;
import java.util.*;

public class CODEFORCES {
	private InputStream is;
	private PrintWriter out;

	void Merge(int a[], int p, int r) {
		if (p < r) {
			int q = p + r >> 1;
			Merge(a, p, q);
			Merge(a, q + 1, r);
			Merge_Array(a, p, q, r);
		}
	}

	void Merge_Array(int a[], int p, int q, int r) {
		int b[] = new int[q - p + 1];
		int c[] = new int[r - q];
		for (int i = 0; i < b.length; i++)
			b[i] = a[p + i];
		for (int i = 0; i < c.length; i++)
			c[i] = a[q + i + 1];
		int i = 0, j = 0;
		for (int k = p; k <= r; k++) {
			if (i == b.length) {
				a[k] = c[j];
				j++;
			} else if (j == c.length) {
				a[k] = b[i];
				i++;
			} else if (b[i] < c[j]) {
				a[k] = b[i];
				i++;
			} else {
				a[k] = c[j];
				ans += b.length - i;
				j++;
			}
		}
	}

	long ans = 0;

	int get(int index[], int ind, int val) {
		if (ind > val) {
			if (ind == val + 1)
				return 0;
			int p = g(index, val);
			int q = g(index, ind - 1);
			//out.println(p + " " + q + " " + ind + " " + val);
			return (ind + p) - (q + val + 1);
		} else {
			if (ind == val || ind == val - 1)
				return 0;
			int p = g(index, val - 1);
			int q = g(index, ind);
			//out.println(p + " " + q + " " + ind + " " + val);
			return (val + q) - (p + ind + 1);
		}
	}

	int g(int index[], int val) {
		int l = 0, r = index.length - 1, temp = 0;
		while (l <= r) {
			int mid = l + r >> 1;
			if (index[mid] > val) {
				temp = mid;
				r = mid - 1;
			} else
				l = mid + 1;
		}
		return temp;
	}

	void solve() {
		int n = ni();
		TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
		for (int i = 0; i < n; i++) {
			int x = ni(), y = ni();
			int kx = x, ky = y;
			if (map.containsKey(x))
				kx = map.get(x);
			if (map.containsKey(y))
				ky = map.get(y);
			map.put(x, ky);
			map.put(y, kx);
		}
		int index[] = new int[map.size() + 2], value[] = new int[map.size() + 2];
		int c = 1;
		while (!map.isEmpty()) {
			java.util.Map.Entry<Integer, Integer> e = map.pollFirstEntry();
			index[c] = e.getKey();
			value[c++] = e.getValue();
		}
		index[c] = Integer.MAX_VALUE;
		for (int i = 1; i < c; i++)
			ans += get(index, index[i], value[i]);
		Merge(value, 1, value.length - 2);
		out.println(ans);
	}

	void soln() {
		is = System.in;
		out = new PrintWriter(System.out);
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis() - s + "ms");
	}

	public static void main(String[] args) throws Exception {
		new CODEFORCES().soln();
	}

	// To Get Input
	// Some Buffer Methods
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	private int skip() {
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	private double nd() {
		return Double.parseDouble(ns());
	}

	private char nc() {
		return (char) skip();
	}

	private String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
									// ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n) {
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}

	private int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	private int ni() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

	private void tr(Object... o) {
		if (!oj)
			System.out.println(Arrays.deepToString(o));
	}
}