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 = 100005;
const int MAXX = 1000000000;
long long sol;
int n, cnt, intcnt, totsum;
int p[MAXN * 2], rsum[MAXN * 2], lsum[MAXN * 2], loga[MAXN * 2];
vector<int> V;
map<int, int> a;
pair<int, int> inters[MAXN * 2];
void update(int x) {
  for (; x <= n * 2; x += x & -x) loga[x]++;
}
int query(int x) {
  int ret = 0;
  for (; x; x -= x & -x) ret += loga[x];
  return ret;
}
int inter_sumR(int lo, int val) {
  int hi = intcnt;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (val < inters[mid].first)
      hi = mid - 1;
    else
      lo = mid + 1;
  }
  if (inters[lo].second < val) lo++;
  int rrsum = 0;
  if (lo <= intcnt) rrsum += rsum[lo];
  return totsum - rrsum;
}
int inter_sumL(int hi, int val) {
  int lo = intcnt;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (val > inters[mid].second)
      lo = mid + 1;
    else
      hi = mid - 1;
  }
  if (inters[lo].second > val) lo--;
  int llsum = 0;
  if (lo >= intcnt) llsum += lsum[lo];
  return totsum - llsum;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    V.push_back(x);
    V.push_back(y);
    if (a[x] == 0) {
      a[x] = x;
      p[cnt] = x;
      cnt++;
    }
    if (a[y] == 0) {
      a[y] = y;
      p[cnt] = y;
      cnt++;
    }
    swap(a[x], a[y]);
  }
  sort(p, p + cnt);
  sort(V.begin(), V.end());
  intcnt = cnt;
  cnt--;
  int currint = intcnt;
  inters[intcnt] = (make_pair(p[cnt] + 1, MAXX));
  totsum = MAXX - p[cnt];
  rsum[intcnt] = totsum;
  for (int i = cnt; i >= 0; i--) {
    int curr = a[p[i]];
    int currV = lower_bound(V.begin(), V.end(), curr) - V.begin() + 1;
    sol += inter_sumR(currint, curr) + query(currV - 1);
    update(currV);
    if (i && (p[i] - p[i - 1] > 1)) {
      currint--;
      inters[currint] = make_pair(p[i - 1] + 1, p[i] - 1);
      totsum += p[i] - p[i - 1] - 1;
      rsum[currint] = rsum[currint + 1] + inters[currint].second -
                      inters[currint].first + 1;
    }
  }
  if (p[0] > 1) {
    currint--;
    inters[currint] = make_pair(1, p[0] - 1);
  }
  intcnt = currint;
  totsum = inters[currint].second - inters[currint].first + 1;
  lsum[currint] = totsum;
  for (int i = 0; i <= cnt; i++) {
    int curr = a[p[i]];
    if (p[i] > inters[currint].second) sol += inter_sumL(currint, curr);
    if (i < cnt && (p[i + 1] - p[i] > 1) && p[i] > inters[currint].second) {
      currint++;
      lsum[currint] = lsum[currint - 1] + inters[currint].second -
                      inters[currint].first + 1;
      totsum += p[i + 1] - p[i] - 1;
    }
  }
  cout << sol << endl;
  return 0;
}
