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.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.TreeSet;

public class E{
	
	static class BIT {
		long[] arr;

		BIT(int n) {
			arr = new long[n + 1];
		}

		long query (int idx) {
			long ans = 0;
			while(idx > 0) {
				ans += arr[idx];
				idx -= (idx & (-idx));
			}
			return ans;
		}

		void update(int idx, long v) {
			while(idx < arr.length) {
				arr[idx] += v;
				idx += (idx & (-idx));
			}
		}
	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = sc.nextInt();

		long[] a = new long[n];
		long[] b = new long[n];
		
		TreeSet<Long> set = new TreeSet<>();
		set.add(0l);
		
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextLong();
			b[i] = sc.nextLong();
			
			set.add(a[i]);
			set.add(b[i]);
		}
		
		
		ArrayList<Long> A = new ArrayList<>();
		for(long v : set)
			A.add(v);
		HashMap<Long, Integer> map = new HashMap<>();
		ArrayList<Integer> val = new ArrayList<>(); 
		ArrayList<Long> W = new ArrayList<>();
		int c = 1;
		for (int i = 1; i < A.size(); i++) {
			if(A.get(i) > A.get(i - 1) + 1) {
				val.add(c);
				W.add(A.get(i) - A.get(i - 1) - 1);
				c++;
			}
			map.put(A.get(i), c);
			val.add(c);
			W.add(1l);
			c++;
		}
		for (int i = 0; i < n; i++) {
			int idx1 = map.get(a[i]) - 1;
			int idx2 = map.get(b[i]) - 1;
			
			int tmp = val.get(idx1);
			val.set(idx1, val.get(idx2));
			val.set(idx2, tmp);
		}
		BIT t = new BIT(c + 1);
		long ans = 0;
		
		for (int i = c - 2; i >= 0; --i) {
			int x = val.get(i);
			if(x > 0)
				ans += W.get(i) * t.query(x - 1);
			t.update(x, W.get(i));
		}
		
		out.println(ans);
		
		out.flush();
	}


	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while(st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public int nextInt() throws NumberFormatException, IOException {
			return Integer.parseInt(next());
		}
		public long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

	}

}
