#include <bits/stdc++.h>
using namespace std;
struct q {
  int in, num, xx;
};
int n;
int a, b;
map<int, int> m;
vector<pair<int, int> > v;
vector<q> query;
int st[16000005];
void change(int p, int l, int r, int i, int xx) {
  if (r < i || l > i) return;
  if (l == r)
    st[p] = xx;
  else {
    int mid = (l + r) / 2;
    change(p * 2, l, mid, i, xx);
    change(p * 2 + 1, mid + 1, r, i, xx);
    st[p] = st[p * 2] + st[p * 2 + 1];
  }
}
long long sum(int p, int l, int r, int i, int j) {
  if (r < i || l > j) return 0;
  if (l >= i && r <= j) return st[p];
  int mid = (l + r) / 2;
  return sum(p * 2, l, mid, i, j) + sum(p * 2 + 1, mid + 1, r, i, j);
}
bool cmp(q x, q y) { return x.in < y.in; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &a, &b);
    map<int, int>::iterator f = m.find(a);
    map<int, int>::iterator s = m.find(b);
    if (f == m.end() && s == m.end()) {
      m[b] = a;
      m[a] = b;
    } else if (f == m.end()) {
      int x = s->second;
      s->second = a;
      m[a] = x;
    } else if (s == m.end()) {
      int x = f->second;
      f->second = b;
      m[b] = x;
    } else {
      int x = f->second;
      f->second = s->second;
      s->second = x;
    }
  }
  map<int, int>::iterator it = m.begin();
  for (it; it != m.end(); ++it) v.push_back(make_pair(it->second, it->first));
  sort(v.begin(), v.end());
  int cur = 0;
  q y;
  if (v[0].first > 1) {
    y.in = v[0].first - 1;
    y.num = 0;
    y.xx = v[0].first - 1;
    cur++;
    query.push_back(y);
  }
  y.in = v[0].second;
  y.num = cur;
  y.xx = 1;
  cur++;
  query.push_back(y);
  for (int i = 1; i < v.size(); ++i) {
    if (v[i].first - v[i - 1].first > 1) {
      y.in = v[i].first - 1;
      y.num = cur;
      y.xx = (v[i].first - v[i - 1].first) - 1;
      cur++;
      query.push_back(y);
    }
    y.in = v[i].second;
    y.num = cur;
    y.xx = 1;
    cur++;
    query.push_back(y);
  }
  sort(query.begin(), query.end(), cmp);
  long long ans = 0;
  for (int i = 0; i < query.size(); i++) {
    ans += sum(1, 0, cur - 1, query[i].num, cur) * query[i].xx;
    change(1, 0, cur - 1, query[i].num, query[i].xx);
  }
  printf("%I64d\n", ans);
  return 0;
}
