#include <bits/stdc++.h>
using namespace std;
const int N = 400100;
map<int, int> mp, mp2;
map<int, int>::iterator it, it2;
pair<int, int> a[N];
int b[N];
long long t[N];
int f(int x) { return x & -x; }
void add(int x, int y) {
  for (int i = x; i < N; i += f(i)) {
    t[i] += y;
  }
}
long long sum(int x) {
  long long res = 0;
  for (int i = x; i >= 1; i -= f(i)) {
    res += t[i];
  }
  return res;
}
int main() {
  int n, u, v;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u, &v);
    if (!mp[u]) mp[u] = u;
    if (!mp[v]) mp[v] = v;
    int tmp = mp[u];
    mp[u] = mp[v];
    mp[v] = tmp;
  }
  int pre = 1, an = 0;
  for (it = mp.begin(); it != mp.end(); it++) {
    int x = (*it).first, y = (*it).second;
    if (x != 1 && x - pre - 1) {
      a[++an] = pair<int, int>(x - 1, x - pre - 1);
      b[an] = x - 1;
    }
    a[++an] = pair<int, int>(y, 1);
    b[an] = y;
    pre = x;
  }
  sort(b + 1, b + 1 + an);
  for (int i = 1; i <= an; i++) {
    mp2[b[i]] = i;
  }
  for (int i = 1; i <= an; i++) {
    a[i].first = mp2[a[i].first];
  }
  long long res = 0, all = 0;
  for (int i = 1; i <= an; i++) {
    res += 1LL * (all - sum(a[i].first)) * a[i].second;
    add(a[i].first, a[i].second);
    all += a[i].second;
  }
  cout << res << endl;
}
