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.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class inv {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        E x = new E();
        x.solve();
    }
}

class E {
    
    static E.MyScanner in = new E.MyScanner();
    // static Main.Parser sc = new Parser(System.in);
    static PrintWriter out = new PrintWriter(System.out, true);
    
    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 Item implements Comparable<Item> {
        int pos, val;

        public Item(int pos, int val) {
            this.pos = pos;
            this.val = val;
        }

        public int compareTo(Item o) {
            if (pos != o.pos) {
                return Integer.compare(pos, o.pos);
            }
            return Integer.compare(val, o.val);
        }
    }

    static class FenwickTree {
        int n;
        int[] a;

        public FenwickTree(int n) {
            this.n = n;
            a = new int[n];
        }

        private int sum(int r) {
            int res = 0;
            for (int i = r; i >= 0; i = (i & (i + 1)) - 1) {
                res += a[i];
            }
            return res;
        }

        private int sum(int l, int r) {
            return sum(r) - sum(l - 1);
        }

        private void inc(int i) {
            for (; i < n; i |= i + 1) {
                a[i]++;
            }
        }
    }

    public void solve() {
        int q = in.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int i = 0; i < q; i++) {
            int pos1 = in.nextInt() - 1;
            int pos2 = in.nextInt() - 1;
            int num1 = map.containsKey(pos1) ? map.get(pos1) : pos1;
            int num2 = map.containsKey(pos2) ? map.get(pos2) : pos2;
            map.put(pos1, num2);
            map.put(pos2, num1);
        }
        Item[] a = new Item[map.size()];
        int n = 0;
        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
            a[n++] = new Item(e.getKey(), e.getValue());
        }
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            b[i] = a[i].val;
        }
        compress(b);
        long ans = 0;
        FenwickTree tree = new FenwickTree(n);
        for (int i = 0; i < n; i++) {
            ans += tree.sum(b[i], n - 1);
            tree.inc(b[i]);
        }
        for (int idxAfter = 0; idxAfter < n; idxAfter++) {
            int idxBefore = ~Arrays.binarySearch(a, new Item(a[idxAfter].val, -1));
            ans += Math.abs(a[idxAfter].pos - a[idxBefore].pos);
            ans -= Math.abs(idxAfter - idxBefore);
        }
        out.println(ans);
        
        out.close();
    }

    private void compress(int[] a) {
        TreeSet<Integer> set = new TreeSet<Integer>();
        for (int x : a) {
            set.add(x);
        }
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        for (int x : set) {
            int id = map.size();
            map.put(x, id);
        }
        for (int i = 0; i < a.length; i++) {
            a[i] = map.get(a[i]);
        }
    }
}