#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b[N], c[N], fen[N];
vector<int> vec;
inline void add(int p) {
  for (p++; p < N; p += p & -p) fen[p]++;
}
inline long long get(int p) {
  long long ans = 0;
  for (p++; p; p -= p & -p) ans += fen[p];
  return ans;
}
inline void readInput() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
}
inline void compress() {
  for (int i = 0; i < n; i++) {
    vec.push_back(--a[i]);
    vec.push_back(--b[i]);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin();
    b[i] = lower_bound(vec.begin(), vec.end(), b[i]) - vec.begin();
  }
}
inline void solve() {
  iota(c, c + vec.size(), 0);
  for (int i = 0; i < n; i++) swap(c[a[i]], c[b[i]]);
  long long ans = 0;
  for (int i = vec.size() - 1; ~i; add(c[i--]))
    ans += get(c[i]) + abs(vec[i] - vec[c[i]]) - abs(i - c[i]);
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  readInput(), compress(), solve();
  return 0;
}
