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.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class Main {
	static BufferedReader in;
	static PrintWriter out;
	
	static class Point {
		int pos,val;
		public Point(int pos, int val) {
			this.pos = pos;
			this.val = val;
		}		
		
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + pos;
			result = prime * result + val;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Point other = (Point) obj;
			if (pos != other.pos)
				return false;
			if (val != other.val)
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "["+pos+","+val+"]";
		}
	}

	public static long mergeSortInv(int[] a, int low, int high) {
		int size = high - low;
		if (size < 2) return 0;
		long inv = 0;
		int mid = (low + high) >>> 1;
		long inv1 = mergeSortInv(a, low, mid);
		long inv2 = mergeSortInv(a, mid, high);

		int[] b = new int[size];
		int i = low;
		int j = mid;
		for (int k = 0; k < size; k++) {
			if (j<high && a[i]>a[j]) inv+=(mid-i);
			if (i < mid && (j == high || a[i] <= a[j])) {
				b[k] = a[i++];
			} else {
				b[k] = a[j++];
			}
		}
		System.arraycopy(b, 0, a, low, size);
		return inv1+inv2+inv;
	}
	
	public static long getStaticInv(List<Point> pts, int pos) {
		Point p = pts.get(pos);
		if (p.pos==p.val) return 0;
					
		int lo = (p.pos<p.val?pos:0), 
				hi = (p.pos<p.val?pts.size()-1:pos), mid, midPos;
		while (hi-lo>1) {
			mid = (hi+lo) >>> 1;
			midPos = pts.get(mid).pos;
			if (midPos>p.val) {
				hi = mid-1;
			} else {
				lo = mid;
			}
		}
		
		if (pts.get(lo).pos==p.val) return Math.abs(lo-pos)-1;
		return Math.abs(hi-pos)-1;
		
	}
	
	public static void main(String[] args) throws IOException {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);

		Map<Integer,Integer> positions = new HashMap<Integer,Integer>();
		int N = ri();
		int[]a;
		Integer p0, p1;
		for (int i=0;i<N;i++) {
			a = rit();
			p0 = positions.get(a[0]);
			p1 = positions.get(a[1]);
			positions.put(a[0], p1==null?a[1]:p1);
			positions.put(a[1], p0==null?a[0]:p0);
		}
		
		List<Point> pts = new ArrayList<Point>();
		for (Integer i : positions.keySet()) {
			pts.add(new Point(i, positions.get(i)));
		}

		Collections.sort(pts, new Comparator<Point>() {
			@Override
			public int compare(Point o1, Point o2) {				
				return o1.pos-o2.pos;
			}
			
		});
		
		int[]b = new int[pts.size()];		
		for (int i=0;i<pts.size();i++) {
			b[i] = pts.get(i).val;
		}		
		long inv = mergeSortInv(b, 0, b.length);
		long numbers = 0;
		for (int i=0;i<pts.size();i++) {
			numbers = Math.max(0, Math.abs(pts.get(i).pos-pts.get(i).val)-1);
			inv+=(numbers-getStaticInv(pts, i));	
		}
		
		out.println(inv);
	
		//in.close();
		out.close();
	}

	public static int ri() throws IOException {
		return Integer.parseInt(in.readLine());
	}

	public static long rl() throws IOException {
		return Long.parseLong(in.readLine());
	}

	public static String rs() throws IOException {
		return in.readLine();
	}

	public static String[] rst() throws IOException {
		return in.readLine().split(" ");
	}

	public static int[] rit() throws IOException {
		String[] tok = in.readLine().split(" ");
		int[] res = new int[tok.length];
		for (int i = 0; i < tok.length; i++) {
			res[i] = Integer.parseInt(tok[i]);
		}
		return res;
	}

	public static long[] rlt() throws IOException {
		String[] tok = in.readLine().split(" ");
		long[] res = new long[tok.length];
		for (int i = 0; i < tok.length; i++) {
			res[i] = Long.parseLong(tok[i]);
		}
		return res;
	}


}
