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 N = 200005;
int a[N], b[N + N], bit[N], n = 0, nd = 0, sz = 0;
pair<int, int> p[N];
map<int, int> mp;
long long pre[N + N];
int bin1(int val) {
  int l = 0, r = nd - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (b[mid] <= val) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (l > nd - 1) return 0;
  if (l & 1) {
    return pre[nd - 1] - pre[l] + b[l] - val + 1;
  }
  return pre[nd - 1] - pre[l];
}
int bin2(int val) {
  int l = nd, r = sz - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (b[mid] < val) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  l--;
  if (l < nd) return 0;
  if (l & 1) {
    return pre[l] - pre[nd];
  }
  return pre[l] - pre[nd] + val - b[l] + 1;
}
void upd(int x, int val) {
  while (x <= n) {
    bit[x] += val;
    x += x & (-x);
  }
}
int get(int x) {
  int sum = 0;
  while (x > 0) {
    sum += bit[x];
    x -= x & (-x);
  }
  return sum;
}
int main() {
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &a[i], &b[i]);
    mp[a[i]] = a[i];
    mp[b[i]] = b[i];
  }
  for (int i = 0; i < m; i++) {
    swap(mp[a[i]], mp[b[i]]);
  }
  int hold = -1;
  for (auto x : mp) {
    if (hold != -1 && x.first - hold > 1) {
      b[sz++] = hold + 1;
      b[sz++] = x.first - 1;
    }
    hold = x.first;
    p[n] = {x.second, n};
    n++;
  }
  for (int i = 1; i < sz; i++) {
    if (i & 1) {
      pre[i] = pre[i - 1] + b[i] - b[i - 1] + 1;
    } else {
      pre[i] = pre[i - 1];
    }
  }
  sort(p, p + n);
  int nn = n;
  n = 0;
  long long res = 0;
  for (int i = 0; i < nn; i++) {
    a[p[i].second] = n++;
  }
  for (int i = 0; i < nn; i++) {
    res += get(n) - get(a[i] + 1);
    upd(a[i] + 1, 1);
  }
  hold = -1;
  for (auto x : mp) {
    if (hold != -1 && x.first - hold > 1) {
      nd += 2;
    }
    hold = x.first;
    if (nd > 0) {
      res += bin1(x.second);
    }
    if (nd < sz) {
      res += bin2(x.second);
    }
  }
  printf("%I64d", res);
  return 0;
}
