#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = 2e9;
const long long LNF = 9e18;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 10;
int c[MAXN];
int lowbit(int x) { return x & (-x); }
void add(int x, int d) {
  for (; x < MAXN; x += lowbit(x)) c[x] += d;
}
int sum(int x) {
  int ret = 0;
  for (; x > 0; x -= lowbit(x)) ret += c[x];
  return ret;
}
int query(int l, int r) {
  if (l > r) return 0;
  return sum(r) - sum(l - 1);
}
int x[MAXN], y[MAXN], a[MAXN], pre[MAXN], now[MAXN];
map<int, int> M;
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    M.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &x[i], &y[i]);
      a[i] = x[i];
      a[n + i] = y[i];
    }
    sort(a + 1, a + 1 + 2 * n);
    int m = unique(a + 1, a + 1 + 2 * n) - (a + 1);
    memcpy(pre + 1, a + 1, m * sizeof(a[1]));
    memcpy(now + 1, a + 1, m * sizeof(a[1]));
    for (int i = 1; i <= m; i++) M[now[i]] = i;
    for (int i = 1; i <= n; i++) {
      swap(now[M[x[i]]], now[M[y[i]]]);
      swap(M[x[i]], M[y[i]]);
    }
    long long ans = 0;
    memset(c, 0, sizeof(c));
    for (int i = 1; i <= m; i++) {
      int pre_pos = upper_bound(pre + 1, pre + 1 + m, now[i]) - (pre + 1);
      int now_pos = i;
      ans += query(pre_pos + 1, m);
      add(pre_pos, 1);
      if (pre[i] > now[i]) ans += (pre[i] - now[i]) - (now_pos - pre_pos);
      if (pre[i] < now[i]) ans += (now[i] - pre[i]) - (pre_pos - now_pos);
    }
    printf("%lld\n", ans);
  }
}
