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 MAXN = 100 * 1000 + 23;
int n, a[MAXN], b[MAXN], fen[MAXN << 2];
long long ans;
map<int, int> mp;
vector<int> f, g, sad;
void bad(int x, int v) {
  f.push_back(x);
  g.push_back(v);
  sad.push_back(x);
}
void add(int p, int v) {
  for (p++; p <= sad.size(); p += (p & -p)) fen[p] += v;
}
int get(int p) {
  int res = 0;
  for (p++; p; p -= (p & -p)) res += fen[p];
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (!mp.count(a[i])) mp[a[i]] = a[i];
    if (!mp.count(b[i])) mp[b[i]] = b[i];
    swap(mp[a[i]], mp[b[i]]);
  }
  map<int, int>::iterator i = mp.begin();
  while (true) {
    bad(i->second, 1);
    auto j = i;
    j++;
    if (j == mp.end()) break;
    if (j->first != i->first + 1) bad(i->first + 1, j->first - i->first - 1);
    i = j;
  }
  sort(sad.begin(), sad.end());
  int sum = 0;
  for (int i = 0; i < f.size(); i++) {
    f[i] = lower_bound(sad.begin(), sad.end(), f[i]) - sad.begin();
    ans += 1ll * (sum - get(f[i])) * g[i];
    add(f[i], g[i]);
    sum += g[i];
  }
  cout << ans;
  return 0;
}
