#include <bits/stdc++.h>
using namespace std;
map<int, int> ma;
int n, i, a[700010], b[700010], l[700010], r[700010], t, tmp, c[700010];
long long ans, s[700010];
void cc(int x) {
  while (x <= t) {
    c[x] += 1;
    x = x + (x & -x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x = x - (x & -x);
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &a[2 * i - 1], &a[2 * i]);
    l[i] = a[2 * i - 1];
    r[i] = a[2 * i];
  }
  sort(a + 1, a + 1 + 2 * n);
  for (i = 1; i <= 2 * n; i++)
    if (a[i] != a[i - 1]) {
      t++;
      a[t] = a[i];
      b[t] = a[i];
      ma[a[t]] = t;
    }
  for (i = 1; i <= n; i++) {
    tmp = b[ma[l[i]]];
    b[ma[l[i]]] = b[ma[r[i]]];
    b[ma[r[i]]] = tmp;
  }
  for (i = 2; i <= t; i++) s[i] = s[i - 1] + a[i] - a[i - 1] - 1;
  for (i = 1; i <= t; i++)
    if (ma[b[i]] > i)
      ans = ans + s[ma[b[i]]] - s[i];
    else
      ans = ans + s[i] - s[ma[b[i]]];
  for (i = 1; i <= t; i++) {
    cc(ma[b[i]]);
    ans = ans + sum(t) - sum(ma[b[i]]);
  }
  printf("%I64d", ans);
}
