#include <bits/stdc++.h>
using namespace std;
long long int a[200005], b[200005];
long long int ans;
vector<long long int> tree[800005], v[200005];
void build(long long int node, long long int st, long long int ed) {
  if (st == ed) {
    tree[node].push_back(a[st]);
    return;
  }
  long long int mid = (st + ed) / 2;
  build(2 * node, st, mid);
  build(2 * node + 1, mid + 1, ed);
  long long int siz1 = tree[2 * node].size();
  long long int siz2 = tree[2 * node + 1].size();
  tree[node].resize(siz1 + siz2);
  merge(tree[2 * node].begin(), tree[2 * node].end(),
        tree[2 * node + 1].begin(), tree[2 * node + 1].end(),
        tree[node].begin());
}
long long int query(long long int node, long long int st, long long int ed,
                    long long int l, long long int r, long long int x) {
  if (l <= st && ed <= r) {
    return lower_bound(tree[node].begin(), tree[node].end(), x) -
           tree[node].begin();
  }
  if (r < st or ed < l) {
    return 0;
  }
  long long int mid = (st + ed) / 2;
  long long int ans1 = query(2 * node, st, mid, l, r, x);
  long long int ans2 = query(2 * node + 1, mid + 1, ed, l, r, x);
  return (ans1 + ans2);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int m;
  cin >> m;
  map<long long int, long long int> ind, mp;
  while (m--) {
    long long int u, v;
    cin >> u >> v;
    if (mp.find(u) == mp.end()) mp[u] = u;
    if (mp.find(v) == mp.end()) mp[v] = v;
    long long int val = mp[u];
    mp[u] = mp[v];
    mp[v] = val;
  }
  long long int n = (long long int)(mp.size()), i = 0;
  for (auto it : mp) {
    ++i;
    b[i] = it.first;
    ind[b[i]] = i;
    a[i] = it.second;
  }
  build(1, 1, n);
  i = 0;
  for (auto it : mp) {
    ++i;
    ans = ans + query(1, 1, n, i, n, a[i]);
    ans = ans + abs((b[ind[a[i]]] - b[i] + 1) - (ind[a[i]] - i + 1));
  }
  cout << ans;
  return 0;
}
