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.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
	
	static TreeMap<Integer, Integer> map = new TreeMap<>();
	static TreeSet<Integer> set = new TreeSet<>();
	
	public static void main(String[] args) throws IOException {
	
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		int n = sc.nextInt();
		while(n-->0)
		{
			int a = sc.nextInt(), b = sc.nextInt();
			int x = get(a), y = get(b);
			map.put(a, y); map.put(b, x);
			set.add(x); set.add(y);
		}
				
		HashMap<Integer, Integer> comp = new HashMap<>();
		HashMap<Integer, Integer> fixed = new HashMap<>();
		int cnt = 0, lst = 0;
		for(int x: set)
		{
			cnt += x - lst - 1;
			fixed.put(x, cnt);
			comp.put(x, comp.size());
			lst = x;
		}
		
		long ans = 0;
		FenwickTree ft = new FenwickTree(comp.size());
		for(Entry<Integer, Integer> e: map.entrySet())
		{
			int p = e.getKey(), x = e.getValue(), y = comp.get(x);
			if(p <= x)
				ans += x - p + ft.query(y + 1);
			else
			{
				int fx = fixed.get(p) - fixed.get(x);
				int nexc = p - 1 - (ft.query(y + 1) + fx);
				int exc = x - 1 - nexc;
				ans += exc + fx;
			}
			ft.update(y + 1);
		}
		out.println(ans);
		out.close();
	}
	
	static int get(int x)
	{
		Integer y = map.get(x);
		if(y == null)
			return x;
		return y;
	}
	
	static class FenwickTree
	{
		int[] ft;
		int cnt;
		
		FenwickTree(int n) { ft = new int[n + 1]; }
		
		void update(int idx)
		{
			++cnt;
			while(idx < ft.length)
			{
				ft[idx] += 1;
				idx += idx & -idx;
			}
		}
		
		int query(int idx)
		{
			int s = 0;
			while(idx > 0)
			{
				s += ft[idx];
				idx ^= idx & -idx;
			}
			return cnt - s;		
		}
	}
	
	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

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

		public Scanner(String s) throws FileNotFoundException{	br = new BufferedReader(new FileReader(s));}

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

		public int nextInt() throws IOException {return Integer.parseInt(next());}

		public long nextLong() throws IOException {return Long.parseLong(next());}

		public String nextLine() throws IOException {return br.readLine();}

		public double nextDouble() throws IOException { return Double.parseDouble(next()); }

		public boolean ready() throws IOException {return br.ready();} 
	}
}