#include <bits/stdc++.h>
using namespace std;
const int maxx = 2e5 + 20, mxsg = 4 * maxx + 10;
int st[mxsg], n, co = 0;
long long ans = 0;
map<int, int> m;
vector<int> v;
vector<pair<int, int> > q;
int get_sum(int x, int y, int id = 1, int l = 0, int r = 2 * n + 2) {
  if (x >= r || l >= y) return 0;
  if (x <= l && r <= y) return st[id];
  int mid = (l + r) / 2;
  return get_sum(x, y, id * 2, l, mid) + get_sum(x, y, id * 2 + 1, mid, r);
}
void modify(int x, int id = 1, int l = 0, int r = 2 * n + 2) {
  st[id]++;
  if (l + 1 == r) return;
  int mid = (l + r) / 2;
  if (x < mid)
    modify(x, id * 2, l, mid);
  else
    modify(x, id * 2 + 1, mid, r);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b, x, y;
    cin >> a >> b;
    if (m.find(a) != m.end())
      x = m[a];
    else
      x = a;
    if (m.find(b) != m.end())
      y = m[b];
    else
      y = b;
    m[a] = y, m[b] = x;
  }
  for (pair<int, int> x : m) {
    v.push_back(x.first);
  }
  for (pair<int, int> x : m) {
    int e1 = min(x.first, x.second);
    int e2 = max(x.first, x.second);
    int r1 = upper_bound(v.begin(), v.end(), e1) - v.begin();
    int r2 = lower_bound(v.begin(), v.end(), e2) - v.begin();
    int g = r2 - r1;
    g = (e2 - e1 - 1) - g;
    ans += g;
    q.push_back(make_pair(x.second, co));
    co++;
  }
  sort(q.begin(), q.end());
  for (pair<int, int> x : q) {
    ans += get_sum(x.second, 2 * n + 2);
    modify(x.second);
  }
  cout << ans << endl;
  return 0;
}
