#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int maxv = maxn * 4;
int a[maxn], b[maxn], val[maxv];
int sumv[maxv];
int tree[maxv];
int maxnum;
map<int, int> mp;
int lowbit(int x) { return (x & (-x)); }
int Query(int k) {
  int res = 0;
  while (k) {
    res += tree[k];
    k -= lowbit(k);
  }
  return res;
}
void Update(int x, int k) {
  while (x <= maxnum) {
    tree[x] += k;
    x += lowbit(x);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i], &b[i]);
    val[++maxnum] = a[i];
    val[++maxnum] = b[i];
  }
  sort(val + 1, val + 1 + maxnum);
  maxnum = unique(val + 1, val + 1 + maxnum) - val - 1;
  for (int i = 1; i <= maxnum; i++)
    sumv[i] = sumv[i - 1] + val[i] - val[i - 1] - 1;
  for (int i = 1; i <= maxnum; i++) mp[val[i]] = i;
  for (int i = 1; i <= n; i++) {
    int index1 = mp[a[i]], index2 = mp[b[i]];
    swap(val[index1], val[index2]);
  }
  long long ans = 0;
  for (int i = 1; i <= maxnum; i++) {
    int index = mp[val[i]];
    Update(index, 1);
    ans += Query(maxnum) - Query(index);
    ans += abs(sumv[index] - sumv[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
