#include <bits/stdc++.h>
using namespace std;
int n, p[300001], ft[300001];
long long ans;
vector<pair<int, int> > queries;
vector<int> val;
map<int, int> pos;
void inc(int i) {
  while (i <= val.size()) ft[i]++, i += i & -i;
}
int get(int i) {
  int ret = 0;
  while (i > 0) ret += ft[i], i -= i & -i;
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = 0, a, b; i < n; i++) {
    scanf("%d%d", &a, &b);
    val.push_back(a);
    val.push_back(b);
    queries.push_back({a, b});
  }
  sort(val.begin(), val.end());
  val.resize(unique(val.begin(), val.end()) - val.begin());
  for (int i = 0; i < val.size(); i++) pos[val[i]] = i;
  iota(p, p + val.size(), 0);
  for (auto q : queries) swap(p[pos[q.first]], p[pos[q.second]]);
  for (int i = val.size() - 1; i >= 0; i--) ans += get(p[i] + 1), inc(p[i] + 1);
  for (int i = 0; i < val.size(); i++)
    ans += val[max(i, p[i])] - val[min(i, p[i])] - 1 -
           (max(i, p[i]) - min(i, p[i]) - 1);
  printf("%I64d", ans);
}
