#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int num, N;
int sum[maxn * 2];
int X[maxn * 2];
int val[maxn * 2];
int tree[maxn * 2];
struct node {
  int x, y;
} a[maxn * 2];
int getsum(int x) {
  int sum = 0;
  while (x) {
    sum += tree[x];
    x -= (x & (-x));
  }
  return sum;
}
void update(int x, int val) {
  while (x <= num) {
    tree[x] += val;
    x += (x & (-x));
  }
}
int main() {
  while (scanf("%d", &N) != EOF) {
    num = 0;
    for (int i = 1; i <= N; i++) {
      scanf("%d%d", &a[i].x, &a[i].y);
      X[++num] = a[i].x;
      X[++num] = a[i].y;
    }
    memset(tree, 0, sizeof(tree));
    memset(sum, 0, sizeof(sum));
    sort(X + 1, X + num + 1);
    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++) sum[i] += sum[i - 1] + X[i] - X[i - 1] - 1;
    for (int i = 1; i <= N; i++) {
      int l = lower_bound(X + 1, X + num + 1, a[i].x) - X;
      int r = lower_bound(X + 1, X + 1 + num, a[i].y) - X;
      swap(val[l], val[r]);
    }
    long long ans = 0;
    for (int i = 1; i <= num; i++) {
      int pos = lower_bound(X + 1, X + num + 1, val[i]) - X;
      ans += (long long)getsum(num) - getsum(pos - 1);
      ans += (long long)abs(sum[pos] - sum[i]);
      update(pos, 1);
    }
    cout << ans << endl;
  }
  return 0;
}
