#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, MAXN = 100000 * 2 * 2 + 10;
struct IN {
  int a, b;
} in[MAXN];
unordered_map<int, int> num;
int cnt, p[MAXN], s[MAXN], tb[MAXN], tol;
long long sum[MAXN << 1];
inline void insert(int v) {
  if (num.find(v) == num.end()) {
    num[v] = cnt;
    p[cnt++] = v;
  }
}
inline int get_id(int v) { return lower_bound(tb, tb + tol, v) - tb; }
inline int id(int x, int y) { return x + y | x != y; }
inline void push_up(int x, int y, int m) {
  sum[id(x, y)] = sum[id(x, m)] + sum[id(m + 1, y)];
}
void add(int x, int y, int p, int v) {
  if (x == y) {
    sum[id(x, y)] += v;
    return;
  }
  int m = (x + y) >> 1;
  if (p <= m)
    add(x, m, p, v);
  else
    add(m + 1, y, p, v);
  push_up(x, y, m);
}
long long query(int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr) return sum[id(x, y)];
  int m = (x + y) >> 1;
  long long ans = 0;
  if (ql <= m) ans += query(x, m, ql, qr);
  if (qr > m) ans += query(m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n;
  while (~scanf("%d%*c", &n)) {
    cnt = 0;
    num.clear();
    insert(1);
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d %d%*c", &a, &b);
      in[i] = {a, b};
      insert(a), insert(b);
    }
    sort(p, p + cnt);
    p[cnt] = p[cnt - 1] + 1;
    memcpy(s, p, sizeof(int) * (cnt + 1));
    for (int i = 0; i < n; i++) {
      int a = lower_bound(p, p + cnt, in[i].a) - p,
          b = lower_bound(p, p + cnt, in[i].b) - p;
      swap(s[a], s[b]);
    }
    tol = 0;
    for (int i = 0; i < cnt; i++) {
      tb[tol++] = p[i];
      if (p[i + 1] - p[i] > 1) tb[tol++] = p[i] + 1;
    }
    long long ans = 0;
    memset(sum, 0, sizeof(sum));
    for (int i = 0; i < cnt; i++) {
      int v = get_id(s[i]);
      ans += query(0, tol - 1, v, tol - 1);
      add(0, tol - 1, v, 1);
      if (p[i + 1] - p[i] > 1) {
        v = get_id(p[i] + 1);
        ans += query(0, tol - 1, v, tol - 1) * (p[i + 1] - p[i] - 1);
        add(0, tol - 1, v, p[i + 1] - p[i] - 1);
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
