#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
map<int, int> mm;
pair<int, int> pp[100001];
int ar[200001], id[200001];
int bit[200001];
void insert(int x) {
  for (int i = x; i <= 200000; i += i & -i) bit[i]++;
}
int query(int x) {
  int ret = 0;
  for (int i = x; i > 0; i -= i & -i) ret += bit[i];
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = (0); i < (n); ++i) {
    cin >> pp[i].first >> pp[i].second;
    mm[pp[i].first] = mm[pp[i].second] = 1;
  }
  int cnt = 0;
  for (auto &v : mm) {
    v.second = ++cnt;
    id[cnt] = v.first;
  }
  for (int i = (1); i < (cnt + 1); ++i) ar[i] = i;
  for (int i = (0); i < (n); ++i) {
    swap(ar[mm[pp[i].first]], ar[mm[pp[i].second]]);
  }
  long long res = 0;
  for (int i = (1); i < (cnt + 1); ++i) {
    long long v = id[i] - id[i - 1] - 1;
    res += (query(200000) - query(i - 1)) * v;
    res += query(200000) - query(ar[i]);
    if (ar[i] < i) {
      v = id[i] - id[ar[i]] - (i - ar[i]);
      res += v;
    }
    insert(ar[i]);
  }
  cout << res << endl;
}
