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).
/**
 * Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
 */


import java.util.*;

public class Main {
   public static int largestBit(int x) {
      return x & (-x);
   }
   public static int queryBit(int[] _Tree,int _Index) {
      int _Sum = 0;
      for (int i = Math.min(_Tree.length - 1,_Index);i > 0;i -= largestBit(i)) {
         _Sum += _Tree[i];
      }
      return _Sum;
   }
   public static void updateBit(int[] _Tree,int _Index,int _Value) {
      assert _Index > 0 : "Index value <= 0";
      for (int i = _Index; i < _Tree.length; i += largestBit(i)) {
         _Tree[i] += _Value;
      }
   }
   static Scanner sc = new Scanner(System.in);
   public static void main(String[] args) {
      int n = sc.nextInt();
      int[] a = new int[n];
      int[] b = new int[n];
      TreeMap<Integer,Integer> _Hash = new TreeMap();

      for (int i = 0;i < n;i++) {

         a[i] = sc.nextInt();
         b[i] = sc.nextInt();

         if (!_Hash.containsKey(a[i])) {
            _Hash.put(a[i],0);
         }
         if (!_Hash.containsKey(b[i])) {
            _Hash.put(b[i],0);
         }
      }

      int m = _Hash.size();
      int[] _FenwickTree = new int[m + 1];
      int[] _SortedPositions = new int[m];
      int[] _Permutation = new int[m];
      int _Order = 0;

      for (int _Position : _Hash.keySet()) {
         _SortedPositions[_Order] = _Position;
         _Permutation[_Order] = _Position;
         _Hash.put(_Position,_Order);
         ++_Order;
      }

      for (int i = 0;i < n;++i) {
         a[i] = _Hash.get(a[i]);
         b[i] = _Hash.get(b[i]);
         int tmp = _Permutation[a[i]];
         _Permutation[a[i]] = _Permutation[b[i]];
         _Permutation[b[i]] = tmp;
      }

      long _Answer = 0;

      for (int i = m - 1;i >= 0;--i) {
         _Answer += Math.abs(_Permutation[i] - _SortedPositions[i]) - Math.abs(_Hash.get(_Permutation[i]) - i);
         _Answer += queryBit(_FenwickTree,_Hash.get(_Permutation[i]) + 1);
         updateBit(_FenwickTree,_Hash.get(_Permutation[i]) + 1,1);
      }

      System.out.println(_Answer);

   }
}