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;
int bs(int *st, int *en, int val, char c) {
  int k;
  if (c == 'l') k = lower_bound(st, en, val) - st;
  if (c == 'u') k = upper_bound(st, en, val) - st;
  return k;
}
map<int, bool> bi;
map<int, int> M, V;
set<int> s;
int inp[100005][2];
int n;
int pos[2 * 100005];
struct st {
  int pos;
  int val;
  int cval;
  bool operator<(const st &b) const { return pos < b.pos; }
};
st ar[2 * 100005];
int N, lim;
int bit[2 * 100005];
void update(int i) {
  for (int a = i; a <= lim; a = a + (a & -a)) bit[a]++;
}
int q(int i) {
  int s = 0;
  for (int a = i; a >= 1; a = a - (a & -a)) s = s + bit[a];
  return s;
}
int main() {
  M.clear();
  bi.clear();
  s.clear();
  V.clear();
  int a, b, c, d, e, x, y, z;
  scanf("%d", &n);
  for (a = 0; a < n; a++) {
    scanf("%d %d", &inp[a][0], &inp[a][1]);
    s.insert(inp[a][0]);
    s.insert(inp[a][1]);
    if (!bi[inp[a][0]]) {
      x = inp[a][0];
    } else
      x = M[inp[a][0]];
    if (!bi[inp[a][1]]) {
      y = inp[a][1];
    } else
      y = M[inp[a][1]];
    bi[inp[a][0]] = true;
    bi[inp[a][1]] = true;
    M[inp[a][0]] = y;
    M[inp[a][1]] = x;
  }
  set<int>::iterator it;
  N = 0;
  e = 0;
  for (it = s.begin(); it != s.end(); it++) {
    x = *it;
    y = M[x];
    ar[N].pos = x;
    ar[N].val = y;
    N++;
  }
  s.clear();
  for (a = 0; a < N; a++) {
    s.insert(ar[a].val);
  }
  for (it = s.begin(); it != s.end(); it++) {
    V[*it] = ++e;
  }
  lim = e;
  for (a = 0; a < N; a++) {
    ar[a].cval = V[ar[a].val];
    pos[a] = ar[a].pos;
  }
  long long ans;
  ans = 0;
  for (a = 0; a < N; a++) {
    x = q(ar[a].cval);
    update(ar[a].cval);
    ans = ans + (a - x);
    z = (ar[a].val - 1);
    y = bs(pos, pos + N, ar[a].val, 'l');
    z = z - y;
    z = z - (ar[a].pos - 1 - a);
    if (z > 0) ans = ans + z;
    z = (ar[a].pos - 1 - a);
    y = bs(pos, pos + N, ar[a].val, 'l');
    y = (ar[a].val - 1) - (y);
    z = z - y;
    if (z > 0) ans = ans + z;
  }
  cout << ans << endl;
  return 0;
}
