#include <bits/stdc++.h>
using namespace ::std;
const long double PI = acos(-1);
const long long MOD = 1000000000 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long add(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a + b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long mul(long long a, long long b, long long m = MOD) {
  if (a >= m) a %= m;
  if (b >= m) b %= m;
  if (a < 0) a += m;
  if (b < 0) b += m;
  long long res = a * b;
  if (res >= m or res <= -m) res %= m;
  if (res < 0) res += m;
  return res;
}
long long pow_mod(long long a, long long b, long long m = MOD) {
  long long res = 1LL;
  a = a % m;
  while (b) {
    if (b & 1) res = mul(res, a, m);
    b >>= 1;
    a = mul(a, a, m);
  }
  return res;
}
long long fastexp(long long a, long long b) {
  long long res = 1LL;
  while (b) {
    if (b & 1) res = res * a;
    b >>= 1;
    a *= a;
  }
  return res;
}
int gcdExtendido(int a, int b, int *x, int *y) {
  if (a == 0) {
    *x = 0;
    *y = 1;
    return b;
  }
  int x1, y1;
  int gcd = gcdExtendido(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
int modInverso(int a, int m) {
  int x, y;
  int g = gcdExtendido(a, m, &x, &y);
  if (g != 1)
    return -1;
  else
    return (x % m + m) % m;
}
const int N = 100000 + 5;
const int M = 600000 + 5;
int n;
int L[N];
int R[N];
int w[M];
int p[M];
int ft[M];
int len = 0;
void compress() {
  set<int> S;
  S.emplace(0);
  S.emplace(2e9);
  for (int i = 1; i <= n; i++) {
    S.emplace(L[i]);
    S.emplace(R[i]);
  }
  vector<int> v;
  for (auto x : S) {
    v.emplace_back(x);
  }
  map<int, int> id;
  for (int i = 1; i < v.size(); i++) {
    w[len++] = v[i] - v[i - 1] - 1;
    w[len] = 1;
    id[v[i]] = len;
    len += 1;
  }
  for (int i = 1; i <= n; i++) {
    L[i] = id[L[i]];
    R[i] = id[R[i]];
  }
}
void update(int pos, int val) {
  pos++;
  while (pos < M) {
    ft[pos] += val;
    pos += (-pos) & pos;
  }
}
int getSum(int pos) {
  int res = 0;
  pos++;
  while (pos > 0) {
    res += ft[pos];
    pos &= pos - 1;
  }
  return res;
}
int main() {
  scanf("%d", &(n));
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &(L[i]), &(R[i]));
  }
  compress();
  for (int i = 0; i < len; i++) p[i] = i;
  for (int i = 1; i <= n; i++) {
    swap(p[L[i]], p[R[i]]);
  }
  long long ans = 0LL;
  int suma = 0;
  for (int i = 0; i < len; i++) {
    int x = p[i];
    ans += 1LL * w[x] * (suma - getSum(x));
    update(x, w[x]);
    suma += w[x];
  }
  printf("%lld\n", ans);
  return 0;
}
