#include <bits/stdc++.h>
using namespace std;
long long n;
map<long long, long long> mm, pos;
struct node {
  int val;
  node *l, *r;
};
node* root;
void update(long long l, long long r, node*& cur = root, long long a = 1,
            long long b = 1e9) {
  if (r < l || r > 1e9) return;
  if (l <= a && b <= r) {
    cur = new node();
    cur->val = (b - a + 1);
    cur->l = cur->r = NULL;
    return;
  }
  if (cur == NULL) cur = new node();
  if (r <= (a + b) / 2)
    update(l, r, cur->l, a, (a + b) / 2);
  else if (l > (a + b) / 2)
    update(l, r, cur->r, (a + b) / 2 + 1, b);
  else {
    update(l, r, cur->l, a, (a + b) / 2);
    update(l, r, cur->r, (a + b) / 2 + 1, b);
  }
  cur->val = 0;
  if (cur->l != NULL) cur->val += cur->l->val;
  if (cur->r != NULL) cur->val += cur->r->val;
}
long long query(long long idx, node* cur = root, long long l = 1,
                long long r = 1e9) {
  if (r < l || r > 1e9) return 0;
  if (cur == NULL) return 0;
  if (l >= idx) return cur->val;
  if (idx > (r + l) / 2) return query(idx, cur->r, (l + r) / 2 + 1, r);
  return query(idx, cur->l, l, (l + r) / 2) +
         query(idx, cur->r, (l + r) / 2 + 1, r);
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    long long a, b;
    cin >> a >> b;
    auto it1 = mm.find(a), it2 = mm.find(b);
    if (it1 == mm.end() && it2 == mm.end())
      mm[a] = b, mm[b] = a;
    else if (it1 == mm.end())
      mm[a] = mm[b], mm[b] = a;
    else if (it2 == mm.end())
      mm[b] = mm[a], mm[a] = b;
    else
      swap(mm[a], mm[b]);
  }
  for (auto p : mm) pos[p.second] = p.first;
  long long ans = 0;
  root = new node();
  root->val = 0;
  root->l = root->r = NULL;
  long long prev = -1;
  for (auto p : pos) {
    if (prev != -1 && p.first != prev + 1) {
      update(prev + 1, p.first - 1);
      ans += (p.first - prev - 1) * query(p.first);
    }
    prev = p.first;
    ans += query(p.second + 1);
    update(p.second, p.second);
  }
  cout << ans << endl;
}
