#include <bits/stdc++.h>
using namespace std;
typedef struct fenvick {
  int data[200200];
  void update(int n, int a) {
    if (n == 0)
      data[0] += a;
    else {
      for (int i = n; i <= 200100; i += i & (-i)) {
        data[i] += a;
      }
    }
  }
  int sum(int n) {
    int k = 0;
    if (n == 0)
      return data[0];
    else {
      for (int i = n; i > 0; i -= i & (-i)) {
        k += data[i];
      }
    }
    return data[0] + k;
  }
} fenvick;
map<int, int> m;
map<int, int>::iterator it;
vector<int> vp;
vector<int>::iterator it2, it3;
pair<int, int> p;
vector<pair<int, int> > v;
int n, a, b, i, c, s, k1, k2;
long long sum;
fenvick fen;
int main() {
  scanf("%d", &n);
  sum = 0;
  for (i = 0; i < n; i++) {
    scanf("%d %d", &a, &b);
    it = m.find(a);
    if (it == m.end()) m[a] = a;
    it = m.find(b);
    if (it == m.end()) m[b] = b;
    c = m[a];
    m[a] = m[b];
    m[b] = c;
  }
  for (it = m.begin(); it != m.end(); it++) {
    vp.push_back(it->first);
    p.first = -(it->second);
    p.second = (it->first);
    v.push_back(p);
  }
  sort(v.begin(), v.end());
  for (i = 0; i < v.size(); i++) {
    it2 = lower_bound(vp.begin(), vp.end(), v[i].second);
    k1 = it2 - vp.begin();
    it3 = lower_bound(vp.begin(), vp.end(), -v[i].first);
    k2 = it3 - vp.begin();
    if (-v[i].first < v[i].second) {
      sum += v[i].second + v[i].first - k1 + k2;
    } else {
      sum += -v[i].first - v[i].second - k2 + k1;
    }
    sum += fen.sum(k1);
    fen.update(k1, 1);
  }
  printf("%lld\n", sum);
  return 0;
}
