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 long long N = 2e5 + 10, MOD = 1e9 + 7, INF = 1e9 + 10, SQ = 550, LOG = 25;
long long n, m, x1, x2, d, z, t, y2, a[N], dp[N], c[N], b[N], mini, ptr, x, y,
    ptr1, ptr2, ans;
pair<long long, long long> pr[N];
map<long long, long long> mp;
set<long long> s;
vector<long long> v;
void add(long long x, long long y) {
  for (; x < N; x += x & (-x)) dp[x] += y;
}
long long sum(long long x) {
  long long answer = 0;
  for (; x; x -= x & (-x)) answer += dp[x];
  return answer;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> pr[i].first >> pr[i].second;
    pr[i].first--, pr[i].second--;
    s.insert(pr[i].first);
    s.insert(pr[i].second);
  }
  while (s.size()) {
    x = *s.begin();
    s.erase(s.begin());
    d++;
    mp[x] = d;
    a[d] = x;
    v.push_back(d);
  }
  for (int i = 0; i < v.size(); i++) {
    b[i] = v[i];
    c[v[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    swap(v[mp[pr[i].first] - 1], v[mp[pr[i].second] - 1]);
  }
  for (int i = v.size() - 1; i >= 0; i--) {
    ans += sum(v[i] - 1);
    add(v[i], +1);
  }
  for (int i = 0; i < v.size(); i++) {
    x = v[i];
    if (c[x] < i) {
      ans += a[b[i]] - (i - c[x]) - a[x];
    } else if (c[x] > i) {
      ans += a[x] - (c[x] - i) - a[b[i]];
    }
  }
  cout << ans;
}
