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 mxn = 2e5 + 5;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
map<long long, long long> mp;
int tree[mxn << 2];
void update(int node, int b, int e, int pos, int val) {
  if (b == e && b == pos) {
    tree[node] = val;
    return;
  }
  int mid = (b + e) >> 1;
  int left = node << 1;
  int right = left | 1;
  if (pos <= mid)
    update(left, b, mid, pos, val);
  else
    update(right, mid + 1, e, pos, val);
  tree[node] = tree[left] + tree[right];
}
int query(int node, int b, int e, int l, int r) {
  if (b > r || e < l) return 0;
  if (b >= l && e <= r) return tree[node];
  int mid = (b + e) >> 1;
  int left = node << 1;
  int right = left | 1;
  return query(left, b, mid, l, r) + query(right, mid + 1, e, l, r);
}
void compress(vector<long long>& v) {
  vector<long long> tmp = v;
  sort(tmp.begin(), tmp.end());
  map<long long, long long> com;
  for (int i = 0; i < tmp.size(); i++) com[tmp[i]] = i + 1;
  for (int i = 0; i < v.size(); i++) v[i] = com[v[i]];
}
long long inversion(vector<long long> v) {
  compress(v);
  long long cnt = 0;
  int n = v.size();
  for (auto it : v) {
    cnt += query(1, 1, n, it, n);
    update(1, 1, n, it, 1);
  }
  return cnt;
}
long long khujo(vector<long long>& pos, long long l, long long r) {
  auto x = upper_bound(pos.begin(), pos.end(), r);
  auto y = lower_bound(pos.begin(), pos.end(), l);
  return x - y;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    long long u, v;
    scanf("%lld%lld", &u, &v);
    if (!mp[u]) mp[u] = u;
    if (!mp[v]) mp[v] = v;
    swap(mp[u], mp[v]);
  }
  vector<long long> val;
  vector<long long> pos;
  for (auto it : mp) {
    if (it.first == it.second) continue;
    val.push_back(it.second);
    pos.push_back(it.first);
  }
  long long ans = inversion(val);
  for (auto it : mp) {
    long long value = it.second;
    long long position = it.first;
    if (value == position) continue;
    if (value > position) swap(value, position);
    ans += position - value - 1;
    ans -= khujo(pos, value + 1, position - 1);
  }
  cout << ans << '\n';
  return 0;
}
