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 double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
const int mx = 1e5 + 5;
int n;
int a[mx], b[mx];
vector<int> store;
map<int, int> mp;
int c[mx * 2], d[mx * 2];
long long ans;
void recur(int L, int R) {
  if (L == R) return;
  int mid = (L + R) / 2;
  recur(L, mid);
  recur(mid + 1, R);
  for (int i = L, j = mid + 1; i <= mid && j <= R;) {
    if (c[i] < c[j])
      ++i;
    else if (c[i] > c[j])
      ans += mid - i + 1, ++j;
    else if (c[i] == c[j])
      ++i;
  }
  inplace_merge(c + L, c + mid + 1, c + R + 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
    store.push_back(a[i]);
    store.push_back(b[i]);
  }
  sort(store.begin(), store.end());
  store.erase(unique(store.begin(), store.end()), store.end());
  int lim = store.size();
  store.insert(store.begin(), -111);
  for (int i = 1; i <= lim; ++i) {
    mp[store[i]] = i;
    c[i] = store[i];
  }
  for (int i = 1; i <= n; ++i) {
    swap(c[mp[a[i]]], c[mp[b[i]]]);
  }
  for (int i = 1; i <= lim; ++i) d[i] = c[i];
  recur(1, lim);
  for (int i = 1; i <= lim; ++i) {
    int le = d[i], ri = store[i];
    if (le > ri) swap(le, ri);
    ans += ri - le - 1;
    le = i, ri = mp[d[i]];
    if (le > ri) swap(le, ri);
    ans -= ri - le - 1;
  }
  cout << ans << '\n';
}
