#include <bits/stdc++.h>
using namespace std;
long long sum[200020], s[200020];
int x[200020];
int a[200020 / 2], b[200020 / 2], rk[200020];
int lowbit(int x) { return x & -x; }
long long get(int k) {
  if (!k) return 0;
  return s[k] + get(k - lowbit(k));
}
void add(int k, int v) {
  if (k > 200020) return;
  s[k] += v;
  add(k + lowbit(k), v);
}
long long solve(int x, int y) { return (long long)abs(sum[y] - sum[x]); }
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    x[i] = a[i];
    x[i + n] = b[i];
    rk[i] = i;
    rk[i + n] = i + n;
  }
  sort(x + 1, x + n + n + 1);
  x[0] = 0;
  int cnt = 0;
  for (int i = 1; i <= n * 2; i++)
    if (i == 1 || x[i] != x[i - 1]) x[++cnt] = x[i];
  sum[0] = 0;
  for (int i = 1; i <= cnt; i++) sum[i] = sum[i - 1] + x[i] - x[i - 1] - 1;
  for (int i = 1; i <= n; i++) {
    int aa = lower_bound(x + 1, x + cnt + 1, a[i]) - x;
    int bb = lower_bound(x + 1, x + cnt + 1, b[i]) - x;
    swap(rk[aa], rk[bb]);
  }
  memset(s, 0, sizeof(s));
  long long ans = 0;
  for (int i = cnt; i > 0; i--) {
    ans += get(rk[i]);
    ans += solve(i, rk[i]);
    add(rk[i], 1);
  }
  printf("%lld", ans);
  return 0;
}
