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.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;

public class E {
	static InputStreamReader isr = new InputStreamReader(System.in);
	static BufferedReader br = new BufferedReader(isr);

	static int[] readIntArray() throws IOException {
		String[] v = br.readLine().split(" ");
		int[] ans = new int[v.length];

		for (int i = 0; i < ans.length; i++) {
			ans[i] = Integer.valueOf(v[i]);
		}

		return ans;
	}

	static int N;
	static int[][] swaps;
	static long inversions = 0;
	static HashMap<Integer, Integer> pos = new HashMap<Integer, Integer>();

	public static void main(String[] args) throws IOException {
		N = readIntArray()[0];
		swaps = new int[N][];
		for (int i = 0; i < N; i++) {
			swaps[i] = readIntArray();
		}

		// long start = System.currentTimeMillis();
		//
		// N = 100000;
		// swaps = new int[N][];
		// for (int i = 0; i < N; i++) {
		// swaps[i] = new int[] { i + 1, 1000000000 - i };
		// }

		for (int i = 0; i < N; i++) {
			int a = swaps[i][0];
			int b = swaps[i][1];

			int ea = a;
			int eb = b;
			if (pos.containsKey(a)) {
				ea = pos.get(a);
			}
			if (pos.containsKey(b)) {
				eb = pos.get(b);
			}
			pos.put(a, eb);
			pos.put(b, ea);
		}

		List<P> points = new ArrayList<P>();
		for (Integer k : pos.keySet()) {
			points.add(new P(k, pos.get(k)));
		}
		Collections.sort(points);
//		System.out.println("time:" + (System.currentTimeMillis() - start));
		int[] vals = new int[points.size()];
		for (int i = 0; i < vals.length; i++) {
			vals[i] = points.get(i).val;
		}
		Arrays.sort(vals);
		HashMap<Integer, Integer> byVal = new HashMap<Integer, Integer>();
		for (int i = 0; i < points.size(); i++) {
			byVal.put(points.get(i).val, i);
		}
		T tree = new T(vals.length);
		HashMap<Integer, Integer> greater = new HashMap<Integer, Integer>();
		for (int i = vals.length - 1; i >= 0; i--) {
			int cnt = tree.howMany(0, byVal.get(vals[i]));
			greater.put(byVal.get(vals[i]), cnt);
			tree.set(byVal.get(vals[i]));
		}

		for (int i = 0; i < points.size(); i++) {
			int c = points.get(i).val;

			inversions += greater.get(i);

			P p = points.get(i);
			inversions += Math.max(p.pos - p.val - 1, 0);
			inversions += Math.max(p.val - p.pos - 1, 0);
			if (p.pos > p.val) {
				int p1 = Collections.binarySearch(points, new P(p.val, 0));
				if (p1 < 0) {
					p1 = -(p1 + 1);
				} else {
					p1++;
				}
				int p2 = i;
				inversions -= (p2 - p1);
			} else if (p.pos < p.val) {
				int p1 = Collections.binarySearch(points, new P(p.val, 0));
				if (p1 < 0) {
					p1 = -(p1 + 1);
				}
				int p2 = i + 1;
				inversions -= (p1 - p2);
			}

		}

		System.out.println(inversions);
//		System.out.println("time:" + (System.currentTimeMillis() - start));
	}

	static class T {

		int[] vals;
		int N;

		public T(int N) {
			vals = new int[10 * N];
			this.N = N;
		}

		public int howMany(int from, int to) {
			return howManyInternal(0, 0, N - 1, from, to);
		}

		private int howManyInternal(int root, int rl, int rr, int l, int r) {

			if (r < rl || l > rr)
				return 0;
			if (rl >= l && rr <= r)
				return vals[root];

			int mid = (rl + rr) / 2;
			int ans = 0;
			ans += howManyInternal(2 * root + 1, rl, mid, l, r);
			ans += howManyInternal(2 * root + 2, mid + 1, rr, l, r);

			return ans;
		}

		public void set(int pos) {
			setInternal(0, 0, N - 1, pos);
		}

		private void setInternal(int root, int rl, int rr, int pos) {
			if (pos < rl || pos > rr)
				return;

			vals[root]++;
			if (rl != rr) {
				int mid = (rl + rr) / 2;
				setInternal(2 * root + 1, rl, mid, pos);
				setInternal(2 * root + 2, mid + 1, rr, pos);
			}

		}
	}

	static class P implements Comparable<P> {
		public int pos, val;

		public P(int pos, int val) {
			this.pos = pos;
			this.val = val;
		}

		@Override
		public int compareTo(P a) {
			if (this.pos < a.pos)
				return -1;
			if (this.pos > a.pos)
				return 1;
			return 0;
		}

	}
}
