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.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;


public class E {

	static StringTokenizer st;
	static BufferedReader br;
	static PrintWriter pw;
	static int[]t;
	static int last;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		int n = nextInt();
		ArrayList<Integer> Changed = new ArrayList<Integer>();
		int[]a = new int[n+1], b = new int[n+1];
		Set<Integer> set = new TreeSet<Integer>();
		for (int i = 1; i <= n; i++) {
			a[i] = nextInt();
			b[i] = nextInt();
			if (!set.contains(a[i])) {
				Changed.add(a[i]);
				set.add(a[i]);
			}
			if (!set.contains(b[i])) {
				set.add(b[i]);
				Changed.add(b[i]);
			}
		}
		Collections.sort(Changed);
		Map<Integer, Integer> map = new TreeMap<>();
		last = 1;
		map.put(Changed.get(0), last);
		int[]x = new int[2*n+1];
		x[1] = 1;
		int[]real = new int[2*n+1];
		real[1] = Changed.get(0);
		for (int i = 1; i < Changed.size(); i++) {
			last++;
			x[last] = last;
			real[last] = Changed.get(i);
			map.put(Changed.get(i), last);
		}
		long ans = 0;
		for (int i = 1; i <= n; i++) {
			int k1 = map.get(a[i]);
			int k2 = map.get(b[i]);
			int t = x[k1];
			x[k1] = x[k2];
			x[k2] = t;
		}
		t = new int[2*n+1];
		for (int i = 1; i <= last; i++) {
			ans += sum(last)-sum(x[i]);
			inc(x[i]);
			int t1 = real[x[i]];
			int t2 = real[i];
			if (t1 != t2) {
				int k1 = Math.min(t1, t2);
				int k2 = Math.max(t1, t2);
				int additional = k2 - k1 - 1;
				int ind1 = Collections.binarySearch(Changed, k1);
				int ind2 = Collections.binarySearch(Changed, k2);
				additional -= ind2-ind1-1;
				ans += additional;
			}
		}
		System.out.println(ans);
		pw.close();
	}
	private static void inc(int ind) {
		for (int i = ind; i <= last; i = (i | (i-1))+1) {
			t[i]++;
		}
	}
	private static int sum(int ind) {
		int sum = 0;
		for (int i = ind; i >= 1; i = (i & (i-1))) {
			sum += t[i];
		}
		return sum;
	}
	private static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	private static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	private static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	private static String next() throws IOException {
		while (st==null || !st.hasMoreTokens())
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}
}
