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).
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> current;
int extract(int index) {
  if (current.find(index) == current.end())
    return index;
  else
    return current[index];
}
int nothing;
long long counter = 0;
vector<int> merge(vector<int> a, vector<int> b) {
  vector<int> filler(a.size() + b.size());
  for (int ia = 0, ib = 0; ia + ib < a.size() + b.size(); nothing = 0) {
    if (ia == a.size()) {
      filler[ia + ib] = b[ib];
      ib++;
    } else if (ib == b.size()) {
      filler[ia + ib] = a[ia];
      ia++;
    } else {
      if (b[ib] > a[ia]) {
        filler[ia + ib] = a[ia];
        ia++;
      } else {
        filler[ia + ib] = b[ib];
        ib++;
        counter += a.size() - ia;
      }
    }
  }
  return filler;
}
vector<int> mergesort(vector<int> *a) {
  if ((*a).size() <= 1) return *a;
  vector<int>::const_iterator start1 = (*a).begin();
  vector<int>::const_iterator end1 = (*a).begin() + ((*a).size() / 2);
  vector<int> part1(start1, end1);
  vector<int>::const_iterator start2 = (*a).begin() + ((*a).size() / 2);
  vector<int>::const_iterator end2 = (*a).end();
  vector<int> part2(start2, end2);
  return merge(mergesort(&part1), mergesort(&part2));
}
int main() {
  int n;
  cin >> n;
  unordered_set<int> seen;
  vector<int> track1(n);
  vector<int> track2(n);
  vector<int> inverted;
  for (int i = 0; i < n; i++) {
    cin >> track1[i] >> track2[i];
    if (seen.find(track1[i]) == seen.end()) {
      inverted.push_back(track1[i]);
      seen.insert(track1[i]);
    }
    if (seen.find(track2[i]) == seen.end()) {
      inverted.push_back(track2[i]);
      seen.insert(track2[i]);
    }
    int temp1 = extract(track2[i]);
    int temp2 = extract(track1[i]);
    current[track1[i]] = temp1;
    current[track2[i]] = temp2;
  }
  sort(inverted.begin(), inverted.end());
  unordered_map<int, int> preserve;
  for (int i = 0; i < inverted.size(); i++) {
    preserve[inverted[i]] = i;
  }
  for (int i = 0; i < inverted.size(); i++) {
    inverted[i] = extract(inverted[i]);
  }
  vector<int> fenwickbaba = mergesort(&inverted);
  long long tempcount = counter;
  for (int i = 0; i < inverted.size(); i++) {
    int curnum = fenwickbaba[i];
    int looknum = extract(curnum);
    if (curnum > looknum) {
      tempcount += curnum - looknum - (preserve[curnum] - preserve[looknum]);
    } else {
      tempcount += looknum - curnum - (preserve[looknum] - preserve[curnum]);
    }
  }
  cout << tempcount << endl;
  return 0;
}
