#include <bits/stdc++.h>
using namespace std;
map<int, int> m, m2, m3;
const int n = 1e6;
int t[n];
int sum(int r) {
  int result = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r];
  return result;
}
void inc(int i) {
  for (; i < n; i = (i | (i + 1))) t[i]++;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (m.find(a) == m.end()) m[a] = a;
    if (m.find(b) == m.end()) m[b] = b;
    swap(m[a], m[b]);
  }
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
    int x = it->first;
    int y = it->second;
    if (x > y) swap(x, y);
    m2[x] = m2[x] + 1;
    m2[y] = m2[y] - 1;
  }
  int s = 0;
  long long res = 0;
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
    s += m2[it->first];
    map<int, int>::iterator it2 = it;
    it2++;
    if (it2 == m.end()) break;
    long long r = it2->first - it->first - 1;
    res += s * r;
  }
  int k = 0;
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    m3[it->first] = k++;
  m2.clear();
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    m2[m3[it->first]] = m3[it->second];
  for (map<int, int>::reverse_iterator it = m2.rbegin(); it != m2.rend();
       it++) {
    res += sum(it->second);
    inc(it->second);
  }
  cout << res;
  return 0;
}
