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;
void sc(int& a) { scanf("%d", &a); }
void sc(long long int& a) { scanf("%lld", &a); }
void sc(int& a, int& b) {
  sc(a);
  sc(b);
}
void sc(int& a, int& b, int& c) {
  sc(a);
  sc(b);
  sc(c);
}
void sc(long long int& a, long long int& b) {
  sc(a);
  sc(b);
}
void sc(long long int& a, long long int& b, long long int& c) {
  sc(a, b);
  sc(c);
}
void write(int& a) { printf("%d", a); }
void write(long long int& a) { printf("%lld", a); }
map<int, int> mp;
vector<int> vec;
int sorted[100005], temp[100005];
long long int merge1(vector<int>&, int, int, int);
long long int newmergesort(vector<int>& v, int left, int right) {
  int mid = (left + right) / 2;
  long long int invcount = (long long int)0;
  if (left < right) {
    invcount += newmergesort(v, left, mid);
    invcount += newmergesort(v, mid + 1, right);
    invcount += merge1(v, left, mid + 1, right);
  }
  return invcount;
}
long long int merge1(vector<int>& v, int left, int mid, int right) {
  int i = left, j = mid, k = left;
  long long int invcnt = (long long int)0;
  while (i <= mid - 1 && j <= right) {
    if (v[i] <= v[j]) {
      temp[k++] = v[i++];
    } else {
      temp[k++] = v[j++];
      invcnt += mid - i;
    }
  }
  while (i <= mid - 1) temp[k++] = v[i++];
  while (j <= right) temp[k++] = v[j++];
  for (i = left; i <= right; i++) v[i] = temp[i];
  return invcnt;
}
int main() {
  int m, n, i, a, b;
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    if (mp[a] == 0) mp[a] = a;
    if (mp[b] == 0) mp[b] = b;
    swap(mp[a], mp[b]);
  }
  map<int, int>::iterator it;
  for (it = mp.begin(); it != mp.end(); it++) {
    if (it->first != it->second) vec.push_back(it->second);
  }
  long long int ans = newmergesort(vec, 0, vec.size() - 1);
  for (it = mp.begin(); it != mp.end(); it++) {
    a = it->first, b = it->second;
    n = abs(a - b) - 1;
    if (a == b) continue;
    if (a > b) swap(a, b);
    int t1 = lower_bound(vec.begin(), vec.end(), a) - vec.begin();
    int t2 = lower_bound(vec.begin(), vec.end(), b) - vec.begin();
    ans += n;
    ans -= (t2 - t1 - 1);
  }
  cout << ans << endl;
}
