#include <bits/stdc++.h>
using namespace std;
int n;
const int mn = (int)2e5 + 10;
int as[mn];
int bs[mn];
set<int> s;
map<int, int> m2, m;
long long sum[mn];
long long tmp[mn];
vector<int> v;
long long cnt_inv(int a, int b) {
  long long ret = 0LL;
  if (a == b) return 0LL;
  int mid = a + b >> 1;
  ret += cnt_inv(a, mid) + cnt_inv(mid + 1, b);
  int k1, k2;
  int t = 0;
  k1 = a;
  k2 = mid + 1;
  while (k1 <= mid && k2 <= b) {
    if (v[k1] > v[k2]) {
      ret += mid - k1 + 1LL;
      tmp[t + a] = v[k2++];
    } else
      tmp[t + a] = v[k1++];
    t++;
  }
  while (k1 <= mid) {
    tmp[t + a] = v[k1];
    k1++;
    t++;
  }
  while (k2 <= b) {
    tmp[t + a] = v[k2];
    t++;
    k2++;
  }
  for (int i = a; i <= b; i++) v[i] = tmp[i];
  return ret;
}
int main() {
  cin >> n;
  m2.clear();
  s.clear();
  for (int i = 0; i < n; i++) {
    cin >> as[i] >> bs[i];
    s.insert(as[i]);
    s.insert(bs[i]);
  }
  v = vector<int>(s.begin(), s.end());
  int sz = v.size();
  sum[0] = v[0] - 1LL;
  for (int i = 0; i < sz; i++) m2[v[i]] = i, sum[i] = v[i] - 1LL - i;
  for (int i = 0; i < n; i++) {
    int aa = m2[as[i]];
    int bb = m2[bs[i]];
    swap(v[aa], v[bb]);
  }
  long long tot = 0LL;
  for (int i = 0; i < sz; i++) {
    int pos = m2[v[i]];
    if (i < pos) {
      tot += sum[pos] - sum[i];
    } else
      tot += sum[i] - sum[pos];
  }
  tot += cnt_inv(0, sz - 1);
  cout << tot << endl;
  return 0;
}
