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 maxx = 600010;
const double pi = acos(-1.0);
const double eps = 1e-15;
set<long long> ss;
set<long long>::iterator it;
map<long long, long long> h, hh;
map<long long, long long>::iterator itt;
long long t, n, m;
long long ans = 0, total = 0, sum = 0, cnt = 0;
long long a[maxx], c[maxx], b[maxx], tr[maxx];
struct node {
  long long l, r;
} s[maxx];
long long lowbit(long long k) { return k & (-k); }
void add(long long x, long long z) {
  while (x <= cnt) {
    tr[x] += z;
    x += lowbit(x);
  }
  return;
}
long long query(long long x) {
  long long res = 0;
  while (x) {
    res += tr[x];
    x -= lowbit(x);
  }
  return res;
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &s[i].l, &s[i].r);
    ss.insert(s[i].l), ss.insert(s[i].r);
  }
  for (it = ss.begin(); it != ss.end(); it++)
    hh[*it] = ++cnt, h[cnt] = *it, c[++sum] = *it;
  sort(c + 1, c + sum + 1);
  for (int i = 1; i <= cnt; i++) b[i] = i;
  for (int i = 1; i <= n; i++) {
    s[i].l = hh[s[i].l], s[i].r = hh[s[i].r];
    swap(b[s[i].l], b[s[i].r]);
  }
  for (int i = 1; i <= cnt; i++) {
    add(b[i], 1);
    if (h[b[i]] > h[i]) {
      long long l1 = 1, r1 = sum, l2 = 1, r2 = sum, op1 = 0, op2 = 0,
                u = h[b[i]] - 1;
      while (l2 <= r2) {
        long long mid = (l2 + r2) >> 1;
        if (c[mid] <= u)
          op2 = mid, l2 = mid + 1;
        else
          r2 = mid - 1;
      }
      u = h[i] + 1;
      while (l1 <= r1) {
        long long mid = (l1 + r1) >> 1;
        if (c[mid] >= u)
          op1 = mid, r1 = mid - 1;
        else
          l1 = mid + 1;
      }
      total = op2 - op1 + 1;
      ans += i - query(b[i]) + h[b[i]] - h[i] - 1 - total;
    } else if (h[b[i]] < h[i]) {
      long long l1 = 1, r1 = sum, l2 = 1, r2 = sum, op1 = 0, op2 = 0,
                u = h[i] - 1;
      while (l2 <= r2) {
        long long mid = (l2 + r2) >> 1;
        if (c[mid] <= u)
          op2 = mid, l2 = mid + 1;
        else
          r2 = mid - 1;
      }
      u = h[b[i]] + 1;
      while (l1 <= r1) {
        long long mid = (l1 + r1) >> 1;
        if (c[mid] >= u)
          op1 = mid, r1 = mid - 1;
        else
          l1 = mid + 1;
      }
      total = op2 - op1 + 1;
      ans += i - query(b[i]) + h[i] - h[b[i]] - 1 - total;
    } else
      ans += i - query(b[i]);
  }
  printf("%lld", ans);
  return 0;
}
