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 long long N = 200004;
long long n, a[N], b[N], id[N], ans = 0, bit[N], dem = 1;
vector<long long> kk, vt;
vector<pair<long long, long long> > vt2;
map<long long, long long> mp;
void build() {
  long long i = 0;
  while (i < kk.size()) {
    long long keep = kk[i];
    vt.push_back(keep);
    while (i < kk.size() && kk[i] == keep) i++;
  }
}
void Swap(long long x, long long y) {
  swap(vt[mp[x]], vt[mp[y]]);
  long long tmp = mp[x];
  mp[x] = mp[y];
  mp[y] = tmp;
}
long long get(long long x) {
  long long res = 0;
  for (long long i = x; i <= dem; i += (i & -i)) res += bit[i];
  return res;
}
void update(long long x) {
  for (long long i = x; i > 0; i -= (i & -i)) bit[i]++;
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    kk.push_back(a[i]);
    kk.push_back(b[i]);
  }
  sort(kk.begin(), kk.end());
  build();
  for (long long i = 0; i < vt.size(); i++) {
    mp[vt[i]] = i;
    id[i] = vt[i];
  }
  for (long long i = 1; i <= n; i++) Swap(a[i], b[i]);
  for (long long i = 0; i < vt.size(); i++)
    vt2.push_back(pair<long long, long long>(vt[i], i));
  sort(vt2.begin(), vt2.end());
  for (long long i = 0; i < vt2.size(); i++) {
    b[i + 1] = vt2[i].first;
  }
  a[vt2[0].second] = 1;
  for (long long i = 1; i < vt2.size(); i++) {
    if (vt2[i].first != vt2[i - 1].first) dem++;
    a[vt2[i].second] = dem;
  }
  for (long long i = 0; i < vt.size(); i++) {
    ans += get(a[i] + 1);
    update(a[i]);
  }
  for (long long i = 0; i < vt.size(); i++) {
    if (vt[i] > id[i] + 1) {
      long long l = id[i] + 1, r = vt[i] - 1;
      long long m1 = lower_bound(b + 1, b + vt.size() + 1, l) - b;
      long long m2 = upper_bound(b + 1, b + vt.size() + 1, r) - b - 1;
      if (m1 <= m2)
        ans += (r - l + 1 - (m2 - m1 + 1));
      else
        ans += r - l + 1;
    }
    if (id[i] > vt[i] + 1) {
      long long l = vt[i] + 1, r = id[i] - 1;
      long long m1 = lower_bound(b + 1, b + vt.size() + 1, l) - b;
      long long m2 = upper_bound(b + 1, b + vt.size() + 1, r) - b - 1;
      if (m1 <= m2)
        ans += (r - l + 1 - (m2 - m1 + 1));
      else
        ans += r - l + 1;
    }
  }
  cout << ans;
}
