#include <bits/stdc++.h>
using namespace std;
const int nm = 100010;
const long long inf = 1000000000;
int n;
map<int, int> vt;
vector<pair<int, int> > xxx;
vector<int> total;
set<int> val;
map<int, int> newval;
int tree[2 * nm];
int demBeHon(int x) {
  int l = 0, r = (int)total.size() - 1, kq = total.size();
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (xxx[mid].second < x) {
      kq = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  if (kq == 0) return total.size() > 0 ? total.back() : 0;
  long long res = total.back() - total[kq - 1];
  if (kq >= 1 && xxx[kq - 1].first < x) res += x - xxx[kq - 1].first;
  return res;
}
int demLonHon(int x) {
  int l = 0, r = (int)total.size() - 1, kq = total.size();
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (xxx[mid].second > x) {
      kq = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  if (kq == 0) return total.size() > 0 ? total.back() : 0;
  long long res = total.back() - total[kq - 1];
  if (kq >= 1 && xxx[kq - 1].second > x) res += xxx[kq - 1].second - x;
  return res;
}
int get(int i) {
  int res = 0;
  while (i > 0) {
    res += tree[i];
    i -= i & (-i);
  }
  return res;
}
void update(int i) {
  while (i < nm + nm) {
    tree[i]++;
    i += i & (-i);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    int tmp = vt[u];
    if (!tmp) {
      tmp = u;
      val.insert(u);
    }
    int tmp2 = vt[v];
    if (!tmp2) {
      tmp2 = v;
      val.insert(v);
    }
    vt[u] = tmp2;
    vt[v] = tmp;
  }
  int cnt = 0;
  for (set<int>::iterator it = val.begin(); it != val.end(); ++it) {
    cnt++;
    newval[*it] = cnt;
  }
  long long res = 0;
  int next = inf;
  for (map<int, int>::reverse_iterator it = vt.rbegin(); it != vt.rend();
       ++it) {
    int i = it->first, x = it->second;
    if (i + 1 <= next) {
      xxx.push_back(make_pair(i + 1, next));
      if (total.size() == 0)
        total.push_back(next - i);
      else
        total.push_back(total.back() + next - i);
    }
    long long tmp = demBeHon(x);
    res += tmp;
    next = it->first - 1;
    res += (long long)get(newval[x] - 1);
    update(newval[x]);
  }
  next = 1;
  xxx.clear();
  total.clear();
  for (map<int, int>::iterator it = vt.begin(); it != vt.end(); ++it) {
    int i = it->first, x = it->second;
    if (i - 1 >= next) {
      xxx.push_back(make_pair(next, i - 1));
      if (total.size() == 0)
        total.push_back(i - next);
      else
        total.push_back(total.back() + i - next);
    }
    long long tmp = demLonHon(x);
    res += tmp;
    next = it->first + 1;
  }
  cout << res << "\n";
}
