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;
static const int N = (1 << 19) + 40;
int a[N];
class tree {
 private:
  int n;

 public:
  tree() {
    n = 0;
    for (int i = 0; i < N; i++) a[i] = 0;
  }
  void setSize(int s) {
    n = 1;
    while (n < s) n *= 2;
  }
  void change(int pos) {
    pos += n;
    a[pos] ^= 1;
    pos /= 2;
    while (pos) {
      a[pos] = a[2 * pos] + a[2 * pos + 1];
      pos /= 2;
    }
  }
  int query(int c, int l, int d, int gl, int gd) {
    if (l >= gl && d <= gd) return a[c];
    if (l >= gd || d <= gl) return 0;
    int mid = (l + d) / 2;
    return query(2 * c, l, mid, gl, gd) + query(2 * c + 1, mid, d, gl, gd);
  }
  int query(int l) { return query(1, 0, n, l + 1, n); }
  void print() {
    for (int i = 1; i < 2 * n; i++) printf("%i ", a[i]);
    printf("\n");
  }
};
struct broj {
  int st;
  int rb;
  int val;
};
broj niz[N];
int tmp[N];
int num = 0;
long long res = 0;
int pronadji(int k) {
  int l = 0;
  int d = num;
  while (l < d - 1) {
    int mid = (l + d) / 2;
    if (niz[mid].rb > k)
      d = mid;
    else
      l = mid;
  }
  return l;
}
bool cmp(broj a, broj b) { return a.val > b.val; }
int main() {
  int n;
  scanf("%i", &n);
  queue<int> st;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%i", &a);
    scanf("%i", &b);
    st.push(a);
    st.push(b);
    tmp[num] = a;
    tmp[num + 1] = b;
    num += 2;
  }
  sort(tmp, tmp + num);
  int cur = num;
  num = 0;
  for (int i = 0; i < cur; i++) {
    niz[num].st = num;
    niz[num].rb = tmp[i];
    niz[num].val = tmp[i];
    num++;
    while (i < cur && tmp[i + 1] == tmp[i]) i++;
  }
  while (!st.empty()) {
    int a = st.front();
    st.pop();
    int b = st.front();
    st.pop();
    int i = pronadji(a);
    int j = pronadji(b);
    swap(niz[i].val, niz[j].val);
  }
  for (int i = 0; i < num; i++) {
    int j = pronadji(niz[i].val);
    int dI = abs(j - i);
    dI -= 1;
    int dO = abs(niz[i].rb - niz[j].rb);
    dO -= 1;
    res += dO - dI;
  }
  sort(niz, niz + num, cmp);
  tree t;
  t.setSize(num);
  for (int i = 0; i < num; i++) {
    res += num - niz[i].st - 1 - t.query(niz[i].st);
    t.change(niz[i].st);
  }
  cout << res << endl;
  return 0;
}
