#include <bits/stdc++.h>
using namespace std;
const long long size = 200007;
const long long modulo = 1e9 + 7;
const long long oo = 1e6;
const double EPS = 1e-11;
const double PI = acos(-1.0);
long long fen[size];
long long sum(int n) {
  long long res = 0;
  for (; n >= 0; n = (n & (n + 1)) - 1) res += fen[n];
  return res;
}
void add(int n) {
  for (; n < size; n |= (n + 1)) fen[n]++;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int m;
  cin >> m;
  int n = 0;
  map<int, int> num;
  vector<pair<int, int> > com(m);
  set<int> pos;
  for (int i = 0; i < m; i++) {
    cin >> com[i].first >> com[i].second;
    pos.insert(com[i].first);
    pos.insert(com[i].second);
  }
  vector<int> a, b;
  for (auto it = pos.begin(); it != pos.end(); it++) {
    b.push_back(a.size());
    a.push_back(*it);
    num[*it] = a.size() - 1;
  }
  for (int i = 0; i < m; i++) {
    int u1 = num[com[i].first], u2 = num[com[i].second];
    swap(b[u1], b[u2]);
  }
  long long ans = 0;
  add(b[0]);
  for (int i = 1; i < b.size(); i++) {
    long long factor = a[i] - a[i - 1] - 1;
    ans += sum(size - 1) - sum(b[i]);
    long long left = sum(size - 1) - sum(i - 1);
    ans += factor * 2 * left;
    add(b[i]);
  }
  cout << ans << endl;
  return 0;
}
