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;
#pragma comment(linker, "/STACK:667772160")
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
struct data {
  int value, id;
  bool operator<(const data& p) const { return value < p.value; }
};
long long arr[200000 + 7], id_value[200000 + 7], tree[3 * 200000 + 7];
pair<int, int> rng[200000 + 7];
map<int, int> ind_rev;
void update(int nd, int l, int r, int id) {
  if (l > id or r < id) return;
  if (l == r) {
    tree[nd] = 1;
    return;
  }
  int mid = (l + r) / 2, lft = 2 * nd, rgt = lft + 1;
  update(lft, l, mid, id);
  update(rgt, mid + 1, r, id);
  tree[nd] = tree[lft] + tree[rgt];
}
int query(int nd, int l, int r, int i, int j) {
  if (r < i or l > j) return 0;
  if (l >= i and r <= j) return tree[nd];
  int mid = (l + r) / 2, lft = 2 * nd, rgt = lft + 1;
  return query(lft, l, mid, i, j) + query(rgt, mid + 1, r, i, j);
}
int main() {
  long long n, all = 1, fn, a, b, p;
  scanf("%lld", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &rng[i].first, &rng[i].second);
    arr[all++] = rng[i].first;
    arr[all++] = rng[i].second;
  }
  sort(arr + 1, arr + all);
  id_value[1] = arr[1];
  ind_rev[arr[1]] = 1;
  fn = all;
  all = 2;
  for (int i = 2; i < fn; i++) {
    if (arr[i] == arr[i - 1]) continue;
    arr[all] = arr[i];
    id_value[all] = arr[i];
    ind_rev[arr[i]] = all;
    all++;
  }
  for (int i = 0; i < n; i++) {
    a = rng[i].first;
    b = rng[i].second;
    a = ind_rev[a];
    b = ind_rev[b];
    swap(arr[a], arr[b]);
  }
  long long ans = 0;
  for (int i = 1; i < all; i++) {
    a = id_value[i];
    b = arr[i];
    b = ind_rev[b];
    fn = b;
    p = b;
    b = query(1, 1, all - 1, b, all - 1);
    if (arr[i] < a) {
      a -= arr[i];
      ans += a;
      fn = i - fn;
      fn = b - fn;
      ans += fn;
    } else
      ans += b;
    update(1, 1, all - 1, p);
    if (i + 1 < all and id_value[i] + 1 != id_value[i + 1]) {
      b = query(1, 1, all - 1, i + 1, all - 1);
      ans += b * (id_value[i + 1] - id_value[i] - 1);
    }
  }
  deb(ans);
  return 0;
}
