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 Main
{
	
	private void solve()throws IOException
	{
		int n=nextInt();
		//map.get(i) is the final position of element i
		HashMap<Integer,Integer> map=new HashMap<>();
		while(n-->0)
		{
			int a=nextInt();
			int b=nextInt();
			if(!map.containsKey(a))
				map.put(a,a);
			if(!map.containsKey(b))
				map.put(b,b);
			int temp=map.get(a);
			map.put(a,map.get(b));
			map.put(b,temp);
		}
		ArrayList<Pair> list=new ArrayList<>();
		for(int i:map.keySet())
			list.add(new Pair(i,map.get(i)));
		Collections.sort(list,new Comparator<Pair>(){
			public int compare(Pair a,Pair b){
				return a.pos-b.pos;
			}
		});
		ArrayList<Pair> orglist=(ArrayList<Pair>)list.clone();
		long ans=count_inversions(list,0,list.size()-1);
		list=orglist;
		for(Pair i:list)
		{
			int init=i.val;
			int curr=i.pos;
			int swapped=floor(Math.max(init,curr),list)-floor(Math.min(init,curr)-1,list);
			int total=Math.max(init,curr)-Math.min(init,curr)+1;
			ans+=total-swapped;
		}
		out.println(ans);
	}
	long count_inversions(ArrayList<Pair> list,int low,int high)
	{
		if(low>=high)
			return 0;
		int mid=(low+high)/2;
		long ret=0;
		ret+=count_inversions(list,low,mid);
		ret+=count_inversions(list,mid+1,high);
		ret+=merge(list,low,mid,high);
		return ret;
	}
	long merge(ArrayList<Pair> list,int low,int mid,int high)
	{
		int i=low,j=mid+1;
		long ret=0;
		ArrayList<Pair> res=new ArrayList<>();
		while(i<=mid && j<=high)
		{
			if(list.get(i).val<=list.get(j).val)
			{
				res.add(list.get(i));
				i++;
			}
			else
			{
				ret+=mid-i+1;
				res.add(list.get(j));
				j++;
			}
		}
		while(i<=mid)
		{
			res.add(list.get(i));
			i++;
		}
		while(j<=high)
		{
			res.add(list.get(j));
			j++;
		}
		for(int p=low;p<=high;p++)
			list.set(p,res.get(p-low));
		return ret;
	}
	int floor(int x,ArrayList<Pair> list)
	{
		int low=0,high=list.size()-1,ans=0;
		while(low<=high)
		{
			int mid=(low+high)/2;
			if(list.get(mid).pos<=x)
			{
				ans=mid+1;
				low=mid+1;
			}
			else
				high=mid-1;
		}
		return ans;
	}
	class Pair{
	    int val,pos;
	    Pair(int a,int b){
	        val=a;
	        pos=b;
	    }
	}

	 
	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}