#include <bits/stdc++.h>
const int pi = acos(-1);
using namespace std;
int const N = 1000000000;
const int M = 500005;
int n, q, freq[M], seg[4 * M];
map<int, int> m, val;
vector<pair<int, int> > v, s;
vector<int> u;
void update(int lo, int hi, int pos, int x) {
  if (lo > x || hi < x) return;
  if (lo == hi) {
    seg[pos] = freq[x];
    return;
  }
  int mid = (lo + hi) / 2;
  update(lo, mid, 2 * pos + 1, x);
  update(mid + 1, hi, 2 * pos + 2, x);
  seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];
}
long long get(int lo, int hi, int pos, int l, int r) {
  if (r < lo || l > hi) return 0;
  if (lo >= l && hi <= r) return seg[pos];
  int mid = (lo + hi) / 2;
  return get(lo, mid, 2 * pos + 1, l, r) + get(mid + 1, hi, 2 * pos + 2, l, r);
}
int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    s.push_back(make_pair(x, y));
    u.push_back(x);
    u.push_back(y);
  }
  sort(u.begin(), u.end());
  u.resize(unique(u.begin(), u.end()) - u.begin());
  int b = 1;
  for (int i = 0; i < u.size(); i++) {
    if (b == u[i]) {
      v.push_back(make_pair(b, b));
    } else {
      v.push_back(make_pair(b, u[i] - 1));
      v.push_back(make_pair(u[i], u[i]));
      b = u[i];
    }
    b++;
  }
  u.clear();
  if (b != N + 1) v.push_back(make_pair(b, N));
  n = v.size();
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first == v[i].second) {
      val[v[i].first] = i;
      m[val[v[i].first]] = i;
    }
    u.push_back(i);
    freq[i] = v[i].second - v[i].first + 1;
  }
  for (int i = 0; i < q; i++) {
    int x = val[s[i].first];
    int y = val[s[i].second];
    swap(m[x], m[y]);
    u[m[x]] = x;
    u[m[y]] = y;
  }
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += get(0, n, 0, 0, u[i]) * freq[u[i]];
    update(0, n, 0, u[i]);
  }
  cout << ans;
  return 0;
}
