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>
#pragma comment(linker, "/STACK:100000000")
using namespace std;
template <class T>
void read(T &x) {
  char c, mi = 0;
  while (c = getchar(), c <= 32)
    ;
  if (c == '-')
    mi = 1, x = 0;
  else if (c < 48 || c > 57)
    return void(x = c);
  else
    x = c - 48;
  while (c = getchar(), c > 32) x = 10 * x + c - 48;
  if (mi == 1) x = -x;
}
template <class T>
void read(T &x, T &y) {
  read(x);
  read(y);
}
template <class T>
void read(T &x, T &y, T &z) {
  read(x, y);
  read(z);
}
template <class T>
void reada(T *a, int n) {
  for (int i = 0; i < n; ++i) read(a[i]);
}
template <class T>
void write(T x) {
  static char s[20];
  char pt = 0, mi = (x < 0);
  if (mi == 1) x = -x;
  while (!pt || x > 0) {
    s[++pt] = (char)(x % 10 + '0');
    x /= 10;
  }
  if (mi == 1) putchar('-');
  while (pt > 0) putchar(s[pt--]);
}
template <class T>
void write(T x, T y) {
  write(x);
  putchar(' ');
  write(y);
}
template <class T>
void write(T x, T y, T z) {
  write(x, y);
  putchar(' ');
  write(z);
}
template <class T>
void writeln(T x) {
  write(x);
  puts("");
}
template <class T>
void writea(T *a, int n) {
  for (int i = 0; i < n; ++i) {
    write(a[i]);
    putchar(i + 1 == n ? '\n' : ' ');
  }
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
T qpom(T a, T b, T mod = 1000000007) {
  T r = 1;
  while (b > 0) {
    if (b & 1) r = r * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return r;
}
template <class T>
T qpow(T a, T b) {
  T r = 1;
  while (b > 0) {
    if (b & 1) r *= a;
    a *= a;
    b /= 2;
  }
  return r;
}
template <class T>
T imin(T a, T b) {
  return a < b ? a : b;
}
template <class T>
T imax(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline void rmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline void rmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
T sqr(const T &a) {
  return a * a;
}
map<int, int> X;
unordered_map<int, int> fen;
int n, mx;
void Swap(int i, int j) {
  if (!X.count(i)) X[i] = i;
  if (!X.count(j)) X[j] = j;
  swap(X[i], X[j]);
}
void upd(int pos, int val) {
  for (; pos < 1000000007; pos |= pos + 1) {
    fen[pos] += val;
  }
}
int get(int pos) {
  int ans = 0;
  for (; pos >= 0; pos &= pos + 1, --pos) {
    if (fen.count(pos)) {
      ans += fen[pos];
    }
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1, x, y; i <= n; ++i) {
    scanf("%d%d", &x, &y);
    Swap(x, y);
    mx = max(mx, y);
    mx = max(mx, x);
  }
  vector<int> all;
  int sz = X.size() - 1;
  long long ans = 0;
  for (map<int, int>::iterator it = X.begin(); it != X.end(); ++it) {
    upd(it->first, 1);
  }
  for (map<int, int>::iterator it = X.begin(); it != X.end(); ++it, --sz) {
    all.push_back(it->second);
    if (it->second > it->first) {
      ans += it->second - it->first - (get(it->second) - get(it->first));
    }
    if (it->first > it->second) {
      ans += it->first - it->second - (get(it->first) - get(it->second));
    }
  }
  fen.clear();
  for (int i = 0; i < all.size(); ++i) {
    upd(all[i], 1);
    ans += i + 1 - get(all[i]);
  }
  cout << ans << endl;
  getchar();
  getchar();
  return 0;
}
