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;
vector<int> v;
map<int, int> ma, value;
int a[100009], sol[1000009], arr[1000009][4], ar[1000009][4];
int bit[1000009][2];
void update(int pos, int add, int n, int o) {
  if (pos < 0) return;
  int ind = pos + 1;
  while (ind <= n) {
    bit[ind][o] += add;
    ind = ind + (ind & (-ind));
  }
}
int query(int pos, int o) {
  if (pos < 0) return 0;
  int ind = pos + 1;
  int sum = 0;
  while (ind != 0) {
    sum += bit[ind][o];
    ind = ind - (ind & (-ind));
  }
  return sum;
}
vector<pair<int, int> > vos;
int main() {
  int n, x, y, timer = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (!value[x]) {
      value[x] = x;
    }
    if (!value[y]) {
      value[y] = y;
    }
    swap(value[x], value[y]);
  }
  for (map<int, int>::iterator it = value.begin(); it != value.end(); it++) {
    v.push_back(it->first);
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (!ma[v[i]]) {
      ma[v[i]] = timer++;
    }
  }
  long long int sol = 0;
  for (map<int, int>::iterator it = value.begin(); it != value.end(); it++) {
    vos.push_back(make_pair(it->first, it->second));
  }
  int prev = vos[vos.size() - 1].first, val = vos[vos.size() - 1].second;
  for (int i = vos.size() - 1; i >= 0; i--) {
    arr[ma[vos[i].first]][0] = query(ma[vos[i].first] - 1, 0);
    arr[ma[vos[i].first]][1] = query(timer - 1, 0) - query(ma[vos[i].first], 0);
    arr[ma[vos[i].first]][2] = query(ma[vos[i].second], 0);
    arr[ma[vos[i].first]][3] =
        query(timer - 1, 0) - query(ma[vos[i].second], 0);
    if (vos[i].first < vos[i].second) {
      sol += vos[i].second - vos[i].first -
             (arr[ma[vos[i].first]][3] - arr[ma[vos[i].second]][1]) -
             (arr[ma[vos[i].first]][2] - arr[ma[vos[i].second]][0]) +
             arr[ma[vos[i].first]][2];
    } else
      sol += arr[ma[vos[i].first]][2];
    update(ma[vos[i].second], 1, timer, 0);
  }
  for (int i = 0; i < vos.size(); i++) {
    ar[ma[vos[i].first]][0] = query(ma[vos[i].first] - 1, 1);
    ar[ma[vos[i].first]][1] = query(timer - 1, 1) - query(ma[vos[i].first], 1);
    ar[ma[vos[i].first]][2] = query(ma[vos[i].second], 1);
    ar[ma[vos[i].first]][3] = query(timer - 1, 1) - query(ma[vos[i].second], 1);
    if (vos[i].first > vos[i].second) {
      sol += vos[i].first - vos[i].second -
             (ar[ma[vos[i].first]][3] - ar[ma[vos[i].second]][1]) -
             (ar[ma[vos[i].first]][2] - ar[ma[vos[i].second]][0]);
    }
    update(ma[vos[i].second], 1, timer, 1);
  }
  cout << sol << endl;
}
