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>
#pragma GCC optimize("O3")
using namespace std;
const int MAX(int &a, int b) { return a = max(a, b); }
const int MIN(int &a, int b) { return a = min(a, b); }
int n, m, r[2 * 100005];
pair<int, int> inds[100005];
map<int, int> to;
map<int, int> inv;
long long bit[4 * 100005];
int sum(int ind) {
  int sm = 0;
  while (ind > 0) {
    sm += bit[ind];
    ind -= ind & -ind;
  }
  return sm;
}
void upd(int ind, int val) {
  while (ind <= m) {
    bit[ind] += val;
    ind += ind & -ind;
  }
}
long long num_inv() {
  long long ct = 0;
  int mx = 0;
  memset(bit, 0, sizeof bit);
  for (int i = m - 1; i >= 0; i--) {
    int q = sum(r[i] - 1);
    ct += q;
    upd(r[i], 1);
  }
  return ct;
}
set<int> exist;
vector<pair<int, int> > lst;
void read() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> inds[i].first >> inds[i].second;
    to[inds[i].first] = inds[i].first;
    to[inds[i].second] = inds[i].second;
    exist.insert(inds[i].first);
    exist.insert(inds[i].second);
  }
  for (int i = 0; i < n; i++) {
    int x = to[inds[i].first];
    int y = to[inds[i].second];
    to[inds[i].first] = y;
    to[inds[i].second] = x;
  }
  int ind = 0;
  for (int x : exist) {
    lst.push_back(make_pair(x, to[x]));
    r[ind] = to[x];
    inv[to[x]] = ind;
    ind++;
  }
  m = ind;
  set<int> q;
  for (int i = 0; i < m; i++) {
    q.insert(r[i]);
  }
  map<int, int> mq;
  ind = 0;
  for (int x : q) {
    mq[x] = ++ind;
  }
  for (int i = 0; i < m; i++) r[i] = mq[r[i]];
}
void solve() {
  long long ret = num_inv();
  for (int i = 0; i < m; i++) {
    int j = lower_bound(lst.begin(), lst.end(), make_pair(lst[i].second, -1)) -
            lst.begin();
    long long w = abs(lst[i].first - lst[j].first) - abs(i - j);
    ret += w;
  }
  cout << ret << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    read();
    solve();
  }
}
