#include <bits/stdc++.h>
using namespace std;
template <class temp>
inline void Rd(temp &p) {
  p = 0;
  char c = 0;
  while (c = getchar(), c < 48)
    ;
  do p = p * 10 + (c & 15);
  while (c = getchar(), c > 47);
}
int n;
map<int, int> Mp;
int res[500005], tot = 0;
struct Binary {
  long long bit[500005];
  void add(int p, long long w) {
    while (p) {
      bit[p] += w;
      p -= p & -p;
    }
  }
  long long sum(int p) {
    long long ans = 0;
    while (p && p <= tot) {
      ans += bit[p];
      p += p & -p;
    }
    return ans;
  }
} tree;
int main() {
  Rd(n);
  for (int i = 1, a, b; i <= n; i++) {
    Rd(a), Rd(b);
    if (Mp.find(a) == Mp.end()) Mp[a] = a;
    if (Mp.find(b) == Mp.end()) Mp[b] = b;
    swap(Mp[a], Mp[b]);
  }
  map<int, int>::iterator it = ++Mp.begin(), ite;
  res[++tot] = Mp.begin()->second;
  for (; it != Mp.end(); ++it) {
    ite = it;
    --ite;
    res[++tot] = it->second;
    if (it->first - ite->first > 1) res[++tot] = ite->first + 1;
  }
  sort(res + 1, res + tot + 1);
  long long ans = 0;
  it = ++Mp.begin();
  tree.add(lower_bound(res + 1, res + tot + 1, Mp.begin()->second) - res, 1);
  for (; it != Mp.end(); ++it) {
    ite = it;
    --ite;
    if (it->first - ite->first > 1) {
      int p = lower_bound(res + 1, res + tot + 1, ite->first + 1) - res;
      long long w = tree.sum(p + 1);
      ans += w * (it->first - ite->first - 1);
      tree.add(p, it->first - ite->first - 1);
    }
    int p = lower_bound(res + 1, res + tot + 1, it->second) - res;
    ans += tree.sum(p + 1);
    tree.add(p, 1);
  }
  cout << ans << endl;
}
