#include <bits/stdc++.h>
using namespace std;
int maxN = 2000002;
class BIT {
  int *ft, size;

 public:
  BIT(int n) {
    size = n;
    ft = new int[n + 1];
    memset(ft, 0, (n + 1) * sizeof(int));
  }
  ~BIT() { delete[] ft; }
  int sum(int b) {
    int sum = 0;
    for (; b; b -= (b & (-b))) sum += ft[b];
    return sum;
  }
  int sum(int a, int b) { return sum(b) - (a == 1 ? 0 : sum(a - 1)); }
  void update(int k, int v) {
    for (; k <= size; k += (k & (-k))) ft[k] += v;
  }
};
long long run(istream &in, ostream &out) {
  set<int> SET;
  map<int, int> MAP;
  int n;
  in >> n;
  int *L = new int[n];
  int *R = new int[n];
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &L[i], &R[i]);
    SET.insert({L[i], R[i]});
  }
  vector<int> IDX(SET.size() + 1);
  vector<int> VAL(SET.begin(), SET.end());
  for (int i = 0; i < IDX.size(); ++i) IDX[i] = i;
  {
    int j = 0;
    for (auto iter : VAL) MAP[iter] = ++j;
  }
  for (int swp = 0; swp < n; ++swp) swap(IDX[MAP[L[swp]]], IDX[MAP[R[swp]]]);
  BIT TREE((int)IDX.size());
  long long ret = 0;
  for (int i = 1; i < IDX.size(); ++i) {
    int idx = IDX[i];
    TREE.update(idx, 1);
    ret += i - TREE.sum(idx);
  }
  vector<int> RDX(IDX.size());
  for (int i = 0; i < IDX.size(); ++i) RDX[IDX[i]] = i;
  for (int i = 1; i < IDX.size(); ++i) {
    int origPos = VAL[i - 1];
    int finalPos = VAL[RDX[i] - 1];
    ret += abs(finalPos - origPos) - abs(i - RDX[i]);
  }
  cout << ret << "\n";
  return 0;
}
int main() {
  ifstream fin("/Users/dung/Documents/workspace/XCodeforces/XCodeforces/input");
  run(cin, cout);
  return 0;
}
