#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, a[N], x[N], y[N], m;
long long ans;
vector<long long> vc, st[N << 2], q;
map<long long, long long> mp;
void readInput() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    vc.push_back(x[i]), vc.push_back(y[i]);
  }
}
void build(long long v = 1, long long s = 0, long long e = m) {
  if (e - s == 1) {
    st[v].push_back(vc[s]);
    return;
  }
  long long mid = (s + e) / 2;
  build(v << 1, s, mid), build(v << 1 | 1, mid, e);
  long long p = s, q = mid;
  while (p != mid || q != e) {
    if (p == mid)
      st[v].push_back(st[v << 1 | 1][q - mid]), q++;
    else if (q == e)
      st[v].push_back(st[v << 1][p - s]), ans += 1LL * q - mid, p++;
    else if (st[v << 1][p - s] > st[v << 1 | 1][q - mid])
      st[v].push_back(st[v << 1 | 1][q - mid]), q++;
    else
      st[v].push_back(st[v << 1][p - s]), ans += 1LL * q - mid, p++;
  }
}
long long get(long long l, long long r, long long val, long long v = 1,
              long long s = 0, long long e = m) {
  if (l >= e || r <= s) return 0;
  if (l <= s && r >= e) {
    long long p = -1, q = e - s - 1;
    if (st[v][q] <= val) return 0;
    while (q - p > 1) {
      long long mid = (q + p) / 2;
      st[v][mid] > val ? q = mid : p = mid;
    }
    return e - s - q;
  }
  long long mid = (s + e) / 2;
  return get(l, r, val, v << 1, s, mid) + get(l, r, val, v << 1 | 1, mid, e);
}
void solve() {
  sort(vc.begin(), vc.end());
  vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
  for (long long i = 0; i < vc.size(); i++) mp[vc[i]] = i + 1;
  m = vc.size();
  for (long long i = 0; i < m; i++) q.push_back(vc[i]);
  for (long long i = 0; i < n; i++) {
    x[i] = mp[x[i]], y[i] = mp[y[i]];
    x[i]--, y[i]--;
    long long z = vc[x[i]];
    vc[x[i]] = vc[y[i]], vc[y[i]] = z;
  }
  for (long long i = 0; i < m; i++) {
    if (q[i] < vc[i])
      ans = ans + vc[i] - (mp[vc[i]] - i) - q[i] + 1;
    else if (q[i] > vc[i])
      ans = ans + q[i] - vc[i] - (i - mp[vc[i]]) - 1;
  }
  build();
  cout << ans << endl;
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  readInput(), solve();
  return 0;
}
