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;
long n, m;
struct o {
  long long x, y;
} b[1000001], a[1000001];
vector<long long> t[1000001];
map<long long, long long> mp, mp1;
void build(int v, int l, int r) {
  if (l == r) {
    t[v].push_back(a[l].x);
    return;
  }
  int mid = (l + r) / 2;
  build(2 * v, l, mid);
  build(2 * v + 1, mid + 1, r);
  for (int k = l; k <= r; ++k) t[v].push_back(a[k].x);
  sort(t[v].begin(), t[v].end());
}
long long tao;
long long ax, bx;
long long get(long long v, long long l, long long r, long long l1,
              long long r1) {
  if (l > r1 || r < l1) return 0;
  if (l1 > r1) return 0;
  if (l == l1 && r == r1) {
    long long lx = 0, rx = t[v].size() - 1;
    while (lx < rx) {
      long long mid = (lx + rx) >> 1;
      if (t[v][mid] >= ax)
        rx = mid;
      else
        lx = mid + 1;
    }
    if (lx > 0)
      if (t[v][lx - 1] >= ax) --lx;
    if (t[v][lx] < ax) ++lx;
    if (lx >= t[v].size()) return 0;
    long long pe = lx;
    lx = 0, rx = t[v].size() - 1;
    while (lx < rx) {
      long long mid = (lx + rx) >> 1;
      if (t[v][mid] <= bx)
        lx = mid + 1;
      else
        rx = mid;
    }
    if (t[v][lx] > bx) --lx;
    long long xt = t[v].size();
    if (xt - 2 >= lx && t[v][lx + 1] <= bx) ++lx;
    long long pe1 = lx;
    if (t[v][pe1] < ax) return 0;
    tao += max(pe1 - pe + 1, 0ll);
    return max(pe1 - pe + 1, 0ll);
  }
  long long mid = (l + r) / 2;
  return get(2 * v, l, mid, l1, min(mid, r1)) +
         get(2 * v + 1, mid + 1, r, max(l1, mid + 1), r1);
}
bool f(o a, o b) { return a.y < b.y; }
bool f1(o a, o b) { return a.x < b.x; }
long s;
long ft(int el, int er) {
  if (el > er) return 0;
  long long ans = 0;
  long l = 1, r = s;
  while (l < r) {
    int mid = (l + r) / 2;
    if (b[mid].x >= el)
      r = mid;
    else
      l = mid + 1;
  }
  if (b[l - 1].x >= el && l > 1) --l;
  if (b[l].x < el) ++l;
  long pe = l;
  l = 1, r = s;
  while (l < r) {
    int mid = (l + r) / 2;
    if (b[mid].x > er)
      r = mid;
    else
      l = mid + 1;
  }
  if (b[l - 1].x <= er) --l;
  if (b[l + 1].x <= er && l < n) ++l;
  long s = l - pe + 1;
  if (l >= pe) {
    if (b[l].x - b[pe].x - s + 1 >= 0) ans = ans + b[l].x - b[pe].x - s + 1;
  }
  if (b[pe].x > b[l].x) {
    ans += er - el + 1;
    return ans;
  }
  if (b[pe].x > el) ans += b[pe].x - el;
  if (er > b[l].x) ans += er - b[l].x;
  return ans;
}
int main() {
  cin >> n;
  for (int k = 1; k <= n; ++k) {
    long a, b;
    cin >> a >> b;
    if (!mp1[a]) mp1[a] = a;
    if (!mp1[b]) mp1[b] = b;
    mp[mp1[a]] = b;
    mp[mp1[b]] = a;
    swap(mp1[a], mp1[b]);
  }
  map<long long, long long>::iterator it;
  s = 0;
  for (it = mp.begin(); it != mp.end(); ++it) {
    a[++s].x = (*it).first;
    a[s].y = (*it).second;
  }
  sort(a + 1, a + s + 1, f);
  for (int k = 1; k <= s; ++k) {
    b[k].x = a[k].x, b[k].y = a[k].y;
  }
  build(1, 1, s);
  sort(b + 1, b + s + 1, f1);
  long long ans = 0;
  for (int k = 1; k <= s; ++k) {
    ax = 0;
    bx = a[k].x - 1;
    tao = 0;
    ans += get(1, 1, s, k, s);
    long el = a[k].y + 1, er = a[k].x - 1;
    ans += ft(a[k].x + 1, a[k].y - 1);
    if (el > er) continue;
    long l = 1, r = s;
    while (l < r) {
      int mid = (l + r) / 2;
      if (b[mid].x >= el)
        r = mid;
      else
        l = mid + 1;
    }
    if (b[l - 1].x >= el && l > 1) --l;
    if (b[l].x < el) ++l;
    long pe = l;
    l = 1, r = s;
    while (l < r) {
      int mid = (l + r) / 2;
      if (b[mid].x > er)
        r = mid;
      else
        l = mid + 1;
    }
    if (b[l - 1].x <= er) --l;
    if (b[l + 1].x <= er && l < n) ++l;
    long s = l - pe + 1;
    if (l >= pe) {
      if (b[l].x - b[pe].x - s + 1 >= 0) ans = ans + b[l].x - b[pe].x - s + 1;
    }
    if (b[pe].x > b[l].x) {
      ans += er - el + 1;
      continue;
    }
    if (b[pe].x > el) ans += b[pe].x - el;
    if (er > b[l].x) ans += er - b[l].x;
  }
  cout << ans;
  return 0;
}
