#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, num;
pair<int, int> a[N + 10];
int X[N * 2 + 10], val[N * 2 + 10], pre[N * 2 + 10];
struct BI {
  int a[2 * N + 10];
  int lowbit(int x) { return x & (-x); }
  void add(int x, int y) {
    while (x <= 2 * N) {
      a[x] += y;
      x += lowbit(x);
    }
  }
  int sum(int x) {
    int now = 0;
    while (x > 0) {
      now += a[x];
      x -= lowbit(x);
    }
    return now;
  }
  int get_sum(int l, int r) { return sum(r) - sum(l - 1); }
} b;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    X[++num] = a[i].first;
    X[++num] = a[i].second;
  }
  sort(X + 1, X + 1 + num);
  num = unique(X + 1, X + 1 + num) - X - 1;
  for (int i = 1; i <= num; i++) val[i] = X[i];
  for (int i = 1; i <= num; i++) pre[i] = pre[i - 1] + X[i] - X[i - 1] - 1;
  for (int i = 1; i <= n; i++) {
    int x = lower_bound(X + 1, X + 1 + num, a[i].first) - X;
    int y = lower_bound(X + 1, X + 1 + num, a[i].second) - X;
    swap(val[x], val[y]);
  }
  long long ans = 0;
  for (int i = 1; i <= num; i++) {
    int pos = lower_bound(X + 1, X + 1 + num, val[i]) - X;
    ans += b.get_sum(pos, num);
    ans += abs(pre[pos] - pre[i]);
    b.add(pos, 1);
  }
  printf("%lld\n", ans);
  return 0;
}
