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;
class FenwickTree {
 private:
  int n;
  vector<int> v;

 public:
  FenwickTree() { n = 0; }
  FenwickTree(int n) {
    this->n = n;
    v.assign(n + 7, 0);
  }
  void update(int x) {
    for (; x <= n; x += x & -x) {
      v[x]++;
    }
  }
  int get(int x) const {
    int res = 0;
    for (; x >= 1; x &= x - 1) {
      res += v[x];
    }
    return res;
  }
};
int n, x, y;
vector<int> seq, pos;
vector<pair<int, int> > sw;
void init() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    pos.push_back(x);
    pos.push_back(y);
    sw.push_back({x, y});
  }
  sort(pos.begin(), pos.end());
  pos.resize(unique(pos.begin(), pos.end()) - pos.begin());
  seq = pos;
  for (auto it = sw.begin(); it != sw.end(); it++) {
    int idX = lower_bound(pos.begin(), pos.end(), it->first) - pos.begin();
    int idY = lower_bound(pos.begin(), pos.end(), it->second) - pos.begin();
    swap(seq[idX], seq[idY]);
  }
}
long long inversion(vector<int> v) {
  vector<int> val;
  for (auto it = v.begin(); it != v.end(); it++) {
    val.push_back(*it);
  }
  sort(val.begin(), val.end());
  val.resize(unique(val.begin(), val.end()) - val.begin());
  FenwickTree bit(val.size());
  long long res = 0;
  for (auto it = v.rbegin(); it != v.rend(); it++) {
    int x = lower_bound(val.begin(), val.end(), *it) - val.begin() + 1;
    if (x > 1) {
      res += bit.get(x - 1);
    }
    bit.update(x);
  }
  return res;
}
int countRange(int l, int r) {
  int L = lower_bound(pos.begin(), pos.end(), l) - pos.begin();
  int R = upper_bound(pos.begin(), pos.end(), r) - pos.begin();
  return (max(R - L, 0));
}
void process() {
  long long res = inversion(seq);
  for (int j = 0; j < (int)seq.size(); j++) {
    int x = seq[j];
    int i = pos[j];
    if (i + 1 <= x - 1) {
      res += x - i - 1 - countRange(i + 1, x - 1);
    }
    if (x + 1 <= i - 1) {
      res += i - x - 1 - countRange(x + 1, i - 1);
    }
  }
  cout << res << '\n';
}
int main() {
  init();
  process();
  return 0;
}
