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;
const int MOD = 1e9 + 7;
const double PI = 3.14159265359;
class SegmentTreePlus {
 public:
  int n;
  vector<long long> vect;
  SegmentTreePlus(int _n) : n(_n) {
    vect.resize(2 * n);
    fill(vect.begin(), vect.end(), 0);
  }
  void modify(int p, long long value) {
    for (vect[p += n] = value; p > 1; p >>= 1) {
      vect[p >> 1] = vect[p] + vect[p ^ 1];
    }
  }
  long long query(int l, int r) {
    long long res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) res += vect[l++];
      if (r & 1) res += vect[--r];
    }
    return res;
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    auto it1 = m.find(x);
    auto it2 = m.find(y);
    if (it1 == m.end() && it2 == m.end()) {
      m[x] = y;
      m[y] = x;
    } else if (it1 == m.end()) {
      int yval = it2->second;
      m[x] = yval;
      m[y] = x;
    } else if (it2 == m.end()) {
      int xval = it1->second;
      m[y] = xval;
      m[x] = y;
    } else {
      int xval = it1->second, yval = it2->second;
      m[x] = yval;
      m[y] = xval;
    }
  }
  vector<int> invar, invpos;
  for (auto p : m) {
    invar.push_back(p.second);
    invpos.push_back(p.first);
  }
  auto v2 = invar;
  sort(v2.begin(), v2.end());
  SegmentTreePlus stp(v2.size());
  long long ans = 0;
  int p0 = lower_bound(v2.begin(), v2.end(), invar[0]) - v2.begin();
  stp.modify(p0, 1);
  for (int i = 1; i < invar.size(); i++) {
    int p = lower_bound(v2.begin(), v2.end(), invar[i]) - v2.begin();
    ans += stp.query(p + 1, invar.size());
    stp.modify(p, 1);
  }
  for (auto p : m) {
    if (p.first < p.second) {
      auto it1 = lower_bound(invpos.begin(), invpos.end(), p.first);
      auto it2 = lower_bound(invpos.begin(), invpos.end(), p.second);
      ans += p.second - p.first - (it2 - it1);
    } else {
      auto it1 = lower_bound(invpos.begin(), invpos.end(), p.second);
      auto it2 = lower_bound(invpos.begin(), invpos.end(), p.first);
      ans += p.first - p.second - (it2 - it1);
    }
  }
  cout << ans;
  return 0;
}
