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.util.*;
public class InfiniteInversions {

	public static void main(String[] args) throws Exception{
		BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(f.readLine());
		
		TreeMap<Integer, Integer> tm = new TreeMap<Integer,Integer>();
		
		for(int i = 0; i < N; i ++) {
			StringTokenizer st = new StringTokenizer(f.readLine());
			
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			
			int x = (tm.containsKey(b)) ? tm.get(b) : b;
			int y = (tm.containsKey(a)) ? tm.get(a) : a;
			
			tm.put(a, x);
			tm.put(b, y);
		}
		
		int size = tm.size();
		int[] keys = new int[size];
		int[] values = new int[size];
		int[] pre = new int[size];
		Pair[] pairs = new Pair[size];
		int counter = 0;
		long ans = 0;
		while(!tm.isEmpty()) {
			int key = tm.firstKey();
			keys[counter] = key;
			values[counter] = tm.get(key);
			pairs[counter] = new Pair(key,tm.get(key));
			pre[counter] = key - counter;
			tm.remove(key);
			
			counter ++;
		}

		for(int i = 0; i < size; i ++) {
			ans += Math.abs(pre[Arrays.binarySearch(keys, pairs[i].x)] - pre[Arrays.binarySearch(keys, pairs[i].y)]);
		}
		
		ans += getInversions(values);	
	
		System.out.println(ans);
		
	}
	
	static long getInversions(int[] a) {
		Pair[] p = new Pair[a.length];
		
		for(int i = 0; i < a.length; i ++) {
			p[i] = new Pair(a[i],i);
		}
		
		Arrays.sort(p);
		
		for(int i = 0; i < a.length; i ++) {
			a[p[i].y] = i;
		}
		
		SegmentTree tree = new SegmentTree(a.length);
		
		long count = 0;
		for(int i = 0; i < a.length; i ++) {
			count += tree.sum(a[i]+1, a.length);
			tree.update(a[i], 1);
		}
		
		return count;
		
		
	}
	
	static class Pair implements Comparable<Pair>{
		int x,y;
		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
		@Override
		public int compareTo(Pair arg0) {
			return x - arg0.x;
		}
	}
	
	static class SegmentTree {
		int n;
		int[] lo, hi, sum;
		public SegmentTree(int n) {
			this.n = n;
			lo = new int[4*n+1];
			hi = new int[4*n+1];
			sum = new int[4*n+1];
			
			init(1,0,n-1);
		}
	
		void update(int i, int val) {
			update(1,i,i,val);
		}
		void update(int i, int a, int b, int val) {
			if(b < lo[i]  || hi[i] < a) {
				return;
			}
			
			if(a <= lo[i] && hi[i] <= b) {
				sum[i] += val;
				return;
			}
		
			update(2*i,a,b,val);
			update(2*i+1,a,b,val);
			
			sum[i] = sum[2*i] + sum[2*i+1];
		}

		void init(int i, int a, int b) {
			lo[i] = a;
			hi[i] = b;
			
			if(a == b) {
				return;
			}
			
			int m = (a+b)/2;
			init(2*i,a,m);
			init(2*i+1,m+1,b);
		}

		public int sum(int i, int j) {
			return sum(1,i,j);
		}
		
		int sum(int i, int a, int b) {
			if(b < lo[i] || hi[i] < a) {
				return 0;
			}
			if(a <= lo[i] && hi[i] <= b) {
				return sum[i];
			}
			
			int sumLeft = sum(2*i,a,b);
			int sumRight = sum(2*i+1,a,b);
			
			return sumLeft + sumRight;
		}
	}

}
