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 A = 100000000000000LL, N = 228228;
vector<long long> c, d;
long long o, e[N], p[N], t[N * 4], po[2], i, j, n, dp[N], m, a[N], b[N];
void modi(long long v, long long l, long long r, long long g) {
  if (l == r) {
    t[v] = 1;
    return;
  }
  long long mid = (l + r) / 2;
  if (g <= mid)
    modi(v * 2, l, mid, g);
  else
    modi(v * 2 + 1, mid + 1, r, g);
  t[v] = t[v * 2] + t[v * 2 + 1];
}
long long get(long long v, long long l, long long r, long long _l,
              long long _r) {
  if (l > r || _l > _r) return 0;
  if (l == _l && r == _r) return t[v];
  long long mid = (l + r) / 2;
  return get(v * 2, l, mid, _l, min(mid, _r)) +
         get(v * 2 + 1, mid + 1, r, max(mid + 1, _l), _r);
}
int main() {
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i] >> b[i], c.push_back(a[i]), c.push_back(b[i]), p[i] = i,
                                                               p[i + n] = i + n;
  sort(c.begin(), c.end());
  d.push_back(c[0]);
  for (i = 1; i < c.size(); i++)
    if (c[i] != c[i - 1]) d.push_back(c[i]);
  c = d;
  dp[0] = c[0] - 1;
  for (i = 1; i < c.size(); i++) dp[i] = dp[i - 1] + c[i] - c[i - 1] - 1;
  m = c.size();
  for (i = 0; i < m; i++) e[i] = c[i];
  for (i = 0; i < n; i++) {
    po[0] = lower_bound(e, e + m, a[i]) - e;
    po[1] = lower_bound(e, e + m, b[i]) - e;
    swap(p[po[0]], p[po[1]]);
  }
  while (m--)
    o += get(1, 1, n * 5, 1, p[m] + 1) + labs(dp[p[m]] - dp[m]),
        modi(1, 1, n * 5, p[m] + 1);
  cout << o << "\n";
}
