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.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;

		// InputStream inputStream = new FileInputStream("input.txt");
		// OutputStream outputStream = new FileOutputStream("output.txt");

		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);

		TaskE solver = new TaskE();
		solver.solve(in, out);

		out.close();
	}
}

class TaskE {

	public void solve(InputReader in, OutputWriter out) {
		
		int n = in.nextInt();
		//int n = 100000;
		TreeMap<Integer,Integer> map = new TreeMap<Integer,Integer>();
		for(int i=0; i<n; i++){
			int a = in.nextInt();
			int b = in.nextInt();
			//int a = i+1;
			//int b = 1000000000-i+1;
			if(!map.containsKey(a)) map.put(a, a);
			if(!map.containsKey(b)) map.put(b, b);
			
			int temp1 = map.remove(a), temp2 = map.remove(b);
			map.put(a, temp2); map.put(b, temp1);
		}
		
		ArrayList<Pair> list = new ArrayList<Pair>();
		int last=0;
		for(Integer key : map.keySet()){
			if(key-last>1){
				list.add(new Pair(last+1,key-last-1));
				last = key-1;
			}
			list.add(new Pair(map.get(key),1));
			last = key;
		}
		
		/*
		for(int i=0; i<list.size(); i++)
			System.out.print(list.get(i).l+"("+list.get(i).size+") ");
		System.out.println();
		*/
		
		int[] temp = new int[list.size()];
		for(int i=0; i<list.size(); i++)
			temp[i] = list.get(i).l;
		Arrays.sort(temp);
		map = new TreeMap<Integer, Integer>();
		int cur=1;
		for(int i=0; i<list.size(); i++){
			map.put(temp[i], cur++);
		}
		
		long ans=0;
		build(list.size()+1);
		for(int i=0; i<list.size(); i++){
			update(map.get(list.get(i).l),list.get(i).size);
		}
		
		for(int i=0; i<list.size(); i++){
			//System.out.println(map.get(list.get(i).l)+" "+sum(0,map.get(list.get(i).l)-1));
			ans+=(long)list.get(i).size*(long)sum(0,map.get(list.get(i).l)-1);
			update(map.get(list.get(i).l),-list.get(i).size);
		}
		
		out.write(ans);
		
	}
	
	int size;
	int[] tree;
	
	void build(int n){
		size=1;
		while(size<n) size<<=1;
		tree = new int[2*size];
	}
	
	int sum(int l, int r){
		l+=size; r+=size;
		int ans=0;
		while(l<=r){
			if(l%2==1) { ans+=tree[l]; l++; }
			if(r%2==0) { ans+=tree[r]; r--; }
			
			l/=2; r/=2;
		}
		
		return ans;
	}
	
	void update(int ind, int val){
		ind+=size; tree[ind]+=val; ind/=2;
		while(ind>=1){
			tree[ind] = tree[2*ind]+tree[2*ind+1];
			ind/=2;
		}
	}
}

class Pair{
	
	public Pair(int l, int size){
		this.l = l;
		this.size = size;
	}
	
	int l,size;
}

class InputReader {

	BufferedReader reader;
	StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		tokenizer = null;
		reader = new BufferedReader(new InputStreamReader(stream));
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException();
			}
		}

		return tokenizer.nextToken();
	}

	public String nextLine() {
		tokenizer = null;
		try {
			return reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException();
		}
	}

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

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

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

	public int[] nextIntArray(int n) {
		int[] res = new int[n];
		for (int i = 0; i < n; i++)
			res[i] = nextInt();

		return res;
	}

}

class OutputWriter {

	PrintWriter out;

	public OutputWriter(OutputStream stream) {
		out = new PrintWriter(
				new BufferedWriter(new OutputStreamWriter(stream)));
	}

	public void write(Object... o) {
		for (Object cur : o)
			out.print(cur);
	}

	public void writeln(Object... o) {
		write(o);
		out.println();
	}

	public void flush() {
		out.flush();
	}

	public void close() {
		out.close();
	}

}
