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;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int, int> typedef pii;
pair<li, li> typedef pli;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
const ld PI = 3.14159265359;
const int mod = 1000000007;
const li inf = 4000000000000000001ll;
inline int power(li _a, int _n) {
  li _c = 1;
  while (_n > 0) {
    if (_n % 2) _c = _c * _a % mod;
    _a = _a * _a % mod;
    _n /= 2;
  }
  return _c;
}
inline int geom(li _a, int _n) {
  if (_a == 0) return 0;
  if (_a == 1) return _n;
  return ((li)power(_a, _n) - 1) * power((_a - 1), mod - 2) % mod;
}
pli _r;
li bigmod(li _a, li _b, li _m) {
  if (_a == 0) return 0;
  if (_a % 2 == 1) return (_b + 2 * bigmod(_a / 2, _b, _m)) % _m;
  return 2 * bigmod(_a / 2, _b, _m) % _m;
}
void euklid(li _a, li _b) {
  if (_b == 1) {
    _r = make_pair(1, -_a + 1);
    return;
  }
  euklid(_b, _a % _b);
  _r = make_pair(_r.second, _r.first - _r.second * (_a / _b));
}
li kinez(li _x, li _a, li _y, li _b) {
  if (_x == _y) return _x;
  if (_a > _b) {
    swap(_a, _b);
    swap(_x, _y);
  }
  euklid(_b, _a);
  (_r.first *= (_x - _y)) %= (_a * _b);
  return (bigmod(_b, _r.first, _a * _b) + _y) % (_a * _b);
}
inline li sum(int _n) { return ((li)_n * (_n + 1)) / 2; }
inline li kv(int _n) { return ((li)_n * (_n + 1) * (2 * _n + 1)) / 6; }
int k = 1;
const int N = 1 << 19;
vector<int> val[N + 3];
int l[N + 3];
int r[N + 3];
int query(int p, int q, int ll, int rr) {
  if (ll > r[p]) return 0;
  if (rr < l[p]) return 0;
  if (ll <= l[p] && rr >= r[p])
    return val[p].end() - upper_bound((val[p]).begin(), (val[p]).end(), q);
  return query(2 * p, q, ll, rr) + query(2 * p + 1, q, ll, rr);
}
li zauzmi(vector<int> niz) {
  li res = 0;
  int n = niz.size();
  while (k < n) k *= 2;
  for (int(i) = (0); (i) < (n); (i)++) {
    val[i + k].push_back(niz[i]);
  }
  for (int(i) = (0); (i) < (k); (i)++) {
    l[i + k] = i;
    r[i + k] = i;
  }
  for (int kk = k / 2; kk; kk /= 2) {
    for (int(i) = (kk); (i) < (kk * 2); (i)++) {
      for (int p1 = 0, p2 = 0;;) {
        if (p1 == val[2 * i].size()) {
          while (p2 < val[2 * i + 1].size())
            val[i].push_back(val[2 * i + 1][p2++]);
          break;
        }
        if (p2 == val[2 * i + 1].size()) {
          while (p1 < val[2 * i].size()) val[i].push_back(val[2 * i][p1++]);
          break;
        }
        if (val[2 * i][p1] < val[2 * i + 1][p2])
          val[i].push_back(val[2 * i][p1++]);
        else
          val[i].push_back(val[2 * i + 1][p2++]);
      }
      l[i] = l[2 * i];
      r[i] = r[2 * i + 1];
    }
  }
  for (int(i) = (1); (i) < (n); (i)++) res += query(1, niz[i], 0, i - 1);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  li res = 0;
  map<int, int> poz;
  int m, a, b;
  cin >> m;
  while (m--) {
    cin >> a >> b;
    if (poz[a] == 0) poz[a] = a;
    if (poz[b] == 0) poz[b] = b;
    swap(poz[a], poz[b]);
  }
  vector<int> zauzet;
  for (auto it : poz) zauzet.push_back(it.first);
  for (int(i) = (0); (i) < (zauzet.size()); (i)++) {
    int x = zauzet[i], y = poz[zauzet[i]];
    if (y < x) {
      res += (x - y);
      res -= (i - (lower_bound((zauzet).begin(), (zauzet).end(), y) -
                   zauzet.begin()));
    } else {
      res += (y - x);
      res -=
          ((lower_bound((zauzet).begin(), (zauzet).end(), y) - zauzet.begin()) -
           i);
    }
  }
  for (int(i) = (0); (i) < (zauzet.size()); (i)++) zauzet[i] = poz[zauzet[i]];
  cout << res + zauzmi(zauzet) << endl;
}
