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.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.print.attribute.standard.Compression;

public class Main {
    
    static class FenwickTree{
        int arr[],size ;
        
        public FenwickTree(int n){
            size = n ;
            arr = new int[n+1];
        }
        
        void update(int idx ,int val){
            while (idx <= size){
                arr[idx] += val;
                idx += (idx & -idx);
            }
        }
        
        int read(int idx){
            int sum = 0;
            while (idx > 0){
                sum += arr[idx];
                idx -= (idx & -idx);
            }
            return sum;
        }
        
    }
    
    static Main.MyScanner sc = new Main.MyScanner();
    // static Main.Parser sc = new Parser(System.in);
    static PrintWriter out = new PrintWriter(System.out, true);

    public static void main(String[] args) throws Exception {
        
        int q = sc.nextInt();
        
        TreeMap<Integer, Integer> hm = new TreeMap<Integer, Integer>();
        for(int i = 0 ; i < q ; i++){
            int pos1 = sc.nextInt(),pos2 = sc.nextInt(),num1 = (hm.containsKey(pos1) ? hm.get(pos1) : pos1 ),num2 = hm.containsKey(pos2) ? hm.get(pos2) : pos2 ;
            hm.put(pos1, num2);
            hm.put(pos2, num1);
        }
        Item a[] = new Item[hm.size()] ;
        int b[] = new int[hm.size()] ;
        int n = 0 ;
        for(Map.Entry<Integer, Integer> e : hm.entrySet()){
            a[n] = new Item(e.getKey(), e.getValue()) ;
            b[n++] = e.getValue() ;
        }
        
        compress(b) ;
        FenwickTree ftree = new FenwickTree(b.length+1);
        long ans = 0 ;
        for(int i = b.length-1 ; i >= 0 ; i--){
            ans += ftree.read(b[i]-1) ;
            ftree.update(b[i], 1);
        }
        
        for(int i = 0 ; i < a.length ; i++){
            int patnerInArray = Arrays.binarySearch(a, new Item(a[i].val, -1));
            ans += Math.abs(a[i].pos - a[patnerInArray].pos) ;
            ans -= Math.abs(i - patnerInArray) ;
        }
        out.println(ans);
        out.close();
    }

    private static void compress(int[] b) {
        // TODO Auto-generated method stub
        TreeSet<Integer> set = new TreeSet<Integer>();
        for(int x : b ) set.add(x) ;
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>() ;
        for(Integer x : set) map.put(x, map.size()+1) ;
        for(int i = 0 ; i < b.length ; i++){
            b[i] = map.get(b[i]) ;
        }
    }

    static private class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public int mod(long x) {
            return (int) x % 1000000007;
        }

        public int mod(int x) {
            return x % 1000000007;
        }

        boolean hasNext() {
            if (st.hasMoreElements())
                return true;
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return st.hasMoreTokens();
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

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

    static class Parser {
        final private int BUFFER_SIZE = 65536; // 2^16, a good compromise for
                                                // some problems
        private InputStream din; // Underlying input stream
        private byte[] buffer; // Self-maintained buffer
        private int bufferPointer; // Current read position in the buffer
        private int bytesRead; // Effective bytes in the buffer read from the
                                // input stream

        public Parser(InputStream in) {
            din = in;
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        /**
         * Read the next integer from the input stream.
         * 
         * @return The next integer.
         * @throws IOException
         */
        public int nextInt() throws IOException {
            int result = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            while (c >= '0' && c <= '9') {
                result = result * 10 + c - '0';
                c = read();
            }
            if (neg)
                return -result;
            return result;
        }

        /**
         * Read the next byte of data from the input stream.
         * 
         * @return the next byte of data, or -1 if the end of the stream is
         *         reached.
         * @throws IOException
         *             if an I/O error occurs.
         */
        public byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        /**
         * Read data from the input stream into the buffer
         * 
         * @throws IOException
         *             if an I/O error occurs.
         */
        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }
    }
}

class Item implements Comparable<Item>{
    int pos,val;
    
    public Item(int pos,int val){
        this.pos = pos ;
        this.val = val ;
    }

    @Override
    public int compareTo(Item o) {
        // TODO Auto-generated method stub
        return this.pos - o.pos ;
    }
}