#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5, mod = 1e9 + 7, sz = 750, inf = 2e9 + 100, lg = 20;
long long a[N], b[N], t[N];
map<long long, long long> m;
vector<pair<long long, long long> > v;
vector<long long> st;
void upd(long long p) {
  for (long long x = p; x < N; x = (x | (x + 1))) t[x]++;
}
long long get(long long p) {
  long long res = 0;
  for (long long x = p; x >= 0; x = (x & (x + 1)) - 1) res += t[x];
  return res;
}
long long sum(long long l, long long r) {
  if (l > r) return 0;
  return get(r) - get(l - 1);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (m.find(a[i]) == m.end()) m[a[i]] = a[i];
    if (m.find(b[i]) == m.end()) m[b[i]] = b[i];
    swap(m[a[i]], m[b[i]]);
  }
  for (auto &e : m) {
    v.push_back(e);
    st.push_back(e.first);
  }
  sort((st).begin(), (st).end());
  st.erase(unique((st).begin(), (st).end()), st.end());
  long long cnt = 0, ans = 0;
  for (auto u : v) {
    long long p = u.first, x = u.second;
    x = lower_bound((st).begin(), (st).end(), x) - st.begin();
    ans += cnt - get(x);
    cnt++;
    upd(x);
  }
  for (auto u : v) {
    long long p = u.first, x = u.second, x1, p1;
    x1 = lower_bound((st).begin(), (st).end(), x) - st.begin();
    p1 = lower_bound((st).begin(), (st).end(), p) - st.begin();
    if (p > x) {
      ans += p - x - 1 - sum(x1 + 1, p1 - 1);
    }
    if (p < x) {
      ans += x - p - 1 - sum(p1 + 1, x1 - 1);
    }
  }
  cout << ans;
}
