#include <bits/stdc++.h>
using namespace std;
long long inv = 0;
void merge(vector<int>& arr, int s, int m, int e) {
  vector<int> res(e - s + 1);
  int i = s, j = m + 1, k = 0;
  while (i <= m && j <= e) {
    if (arr[i] < arr[j]) {
      res[k++] = arr[i++];
    } else {
      inv += (m - i + 1);
      res[k++] = arr[j++];
    }
  }
  while (i <= m) res[k++] = arr[i++];
  while (j <= e) res[k++] = arr[j++];
  for (int i = s; i <= e; i++) {
    arr[i] = res[i - s];
  }
}
void merge_sort(vector<int>& arr, int s, int e) {
  if (e <= s) return;
  int mid = (s + e) / 2;
  merge_sort(arr, s, mid);
  merge_sort(arr, mid + 1, e);
  merge(arr, s, mid, e);
}
int main() {
  int n;
  scanf("%d", &n);
  map<int, int> swaps;
  int a, b;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    int temp = swaps.count(a) ? swaps[a] : a;
    swaps[a] = swaps.count(b) ? swaps[b] : b;
    swaps[b] = temp;
  }
  vector<int> arr;
  for (map<int, int>::iterator it = swaps.begin(); it != swaps.end(); it++) {
    arr.push_back(it->second);
  }
  merge_sort(arr, 0, arr.size() - 1);
  for (map<int, int>::iterator it = swaps.begin(); it != swaps.end(); it++) {
    int cur_pos = it->first;
    int orig_pos = it->second;
    if (cur_pos == orig_pos) continue;
    long long m = abs(orig_pos - cur_pos) - 1;
    int a = cur_pos, b = orig_pos;
    if (a > b) swap(a, b);
    int low = lower_bound(arr.begin(), arr.end(), a) - arr.begin();
    int up = lower_bound(arr.begin() + low, arr.end(), b) - arr.begin();
    m -= up - low - 1;
    inv += m;
  }
  printf("%I64d", inv);
}
