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.util.*;
import java.io.*;
/*
3
1 6
3 4
2 5

2
4 2
1 4
 */
public class b 
{
	public static void main(String[] arg) throws IOException
	{
		new b();
	}
	public b() throws IOException
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = in.nextInt();
		BIT bit = new BIT(300_000);
		// Index and what number is at the index
		TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
		for(int i = 0; i < n; i++)
		{
			int v1 = in.nextInt();
			int v2 = in.nextInt();
			if(!tm.containsKey(v1)) tm.put(v1, v1);
			if(!tm.containsKey(v2)) tm.put(v2, v2);
			int num1 = tm.get(v1);
			int num2 = tm.get(v2);
			tm.put(v1, num2);
			tm.put(v2, num1);
		}
		int[] vs = new int[tm.size()];
		int idx = 0;
		for(int key : tm.keySet())
		{
			vs[idx++] = tm.get(key);
		}
		HashMap<Integer, Integer> save1 = new HashMap<Integer, Integer>();
//		HashMap<Integer, Integer> save2 = new HashMap<Integer, Integer>();
		for(int i = 0; i < vs.length; i++)
		{
			save1.put(vs[i], i);
//			save2.put(i, vs[i]);
		}
		compress(vs);
		long ret = 0;
		for(int i = 0; i < vs.length; i++)
		{
			ret += bit.above(vs[i]);
			bit.update(vs[i], 1);
		}
		for(int v : tm.keySet())
		{
			long op = Math.abs(v-tm.get(v))-1;
			op -= Math.abs(save1.get(v)-save1.get(tm.get(v)))-1;
//			System.out.println(v + " " + tm.get(v));
			ret += op;
		}
		out.println(ret);
		in.close(); out.close();
	}
	void compress(int[] vs)
	{
		TreeSet<Integer> ts = new TreeSet<Integer>();
		for(int v : vs) ts.add(v);
		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
		for(int v : ts) hm.put(v, hm.size()+1);
		for(int i = 0; i < vs.length; i++)
		{
			vs[i] = hm.get(vs[i]);
		}
	}
	class BIT
	{
		int[] vs;
		public BIT(int s)
		{
			vs = new int[s+1];
		}
		public void update(int idx, int v)
		{
			for(int i = idx; i < vs.length; i += (i&-i)) vs[i] += v;
		}
		public int sum(int idx)
		{
			int ret = 0;
			for(int i = idx; i > 0; i -= (i&-i)) ret += vs[i];
			return ret;
		}
		public int above(int idx)
		{
			return sum(vs.length-1)-sum(idx);
		}
	}
	class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;
		public FastScanner(InputStream in)
		{
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer("");
		}
		public String next() throws IOException
		{
			while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
		public void close() throws IOException
		{
			br.close();
		}
		public double nextDouble() throws IOException
		{
			return Double.parseDouble(next());
		}
		public long nextLong() throws IOException
		{
			return Long.parseLong(next());
		}
	}
}
