#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> O[100005];
int a[100005 * 3], px[100005 * 3], s[100005 * 2], m;
vector<int> qq;
map<int, int> pos;
void add(int x) {
  for (x += 3; x <= n + n + 3; x += x & -x) ++s[x];
}
int get(int x) {
  int r = 0;
  for (x += 3; x; x -= x & -x) r += s[x];
  return r;
}
int count(int l, int r) {
  if (l > r) swap(l, r);
  int t1 = r - l + 1;
  int t2 = upper_bound((qq).begin(), (qq).end(), r) -
           lower_bound((qq).begin(), (qq).end(), l);
  return t1 - t2;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < (int)(n); ++i) {
    scanf("%d%d", &O[i].first, &O[i].second);
    qq.push_back(O[i].first), qq.push_back(O[i].second);
  }
  sort((qq).begin(), (qq).end());
  qq.erase(unique((qq).begin(), (qq).end()), qq.end());
  m = qq.size();
  for (int i = 0; i < (int)(n); ++i) {
    int f = O[i].first, s = O[i].second;
    O[i].first = lower_bound((qq).begin(), (qq).end(), O[i].first) - qq.begin();
    O[i].second =
        lower_bound((qq).begin(), (qq).end(), O[i].second) - qq.begin();
    px[O[i].first] = f, px[O[i].second] = s;
  }
  for (int i = 0; i < (int)(m); ++i) a[i] = i, pos[px[i]] = i;
  for (int i = 0; i < (int)(n); ++i) swap(a[O[i].first], a[O[i].second]);
  for (int i = 0; i < (int)(m); ++i) pos[px[a[i]]] = px[i];
  long long res = 0;
  for (int i = (int)(m - 1); i >= (int)(0); --i) {
    res += get(a[i]);
    res += count(pos[px[a[i]]], px[a[i]]);
    add(a[i]);
  }
  cout << res << endl;
  return 0;
}
