#include <bits/stdc++.h>
using namespace std;
struct node {
  int left, right, sum;
  node* child_left;
  node* child_right;
  node() {
    left = 0;
    right = 0;
    sum = 0;
    child_left = nullptr;
    child_right = nullptr;
  }
};
node* build(int l, int r) {
  node* root = new node();
  root->left = l;
  root->right = r;
  if (l == r) {
    root->sum = 0;
    return root;
  }
  int m = (l + r) / 2;
  root->child_left = build(l, m);
  root->child_right = build(m + 1, r);
  root->sum = root->child_left->sum + root->child_right->sum;
  return root;
}
node* update(node* root, int ind, int x) {
  if (root->left > ind || root->right < ind) {
    return root;
  }
  if (root->left == root->right) {
    root->sum += x;
    return root;
  }
  root->child_left = update(root->child_left, ind, x);
  root->child_right = update(root->child_right, ind, x);
  root->sum = root->child_left->sum + root->child_right->sum;
  return root;
}
int get_sum(node* root, int l, int r) {
  if (root->left > r || root->right < l) {
    return 0;
  }
  if (root->left >= l && root->right <= r) {
    return root->sum;
  }
  return get_sum(root->child_left, l, r) + get_sum(root->child_right, l, r);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  long long ans = 0;
  cin >> n;
  map<int, int> g;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    if (!g[a] && !g[b]) {
      g[a] = b;
      g[b] = a;
    } else if (!g[a]) {
      g[a] = g[b];
      g[b] = a;
    } else if (!g[b]) {
      g[b] = g[a];
      g[a] = b;
    } else {
      swap(g[a], g[b]);
    }
  }
  map<int, int> dict;
  int cnt = 1;
  for (auto i : g) {
    if (i.first == i.second) {
      continue;
    }
    dict[i.first] = cnt;
    cnt++;
  }
  node* root = build(1, cnt);
  for (auto i : g) {
    if (i.first == i.second) {
      continue;
    }
    int elem = dict[i.second];
    root = update(root, elem, 1);
    ans += get_sum(root, elem + 1, g.size());
  }
  vector<pair<int, int>> b;
  int last = 1;
  for (auto i : g) {
    if (i.first == i.second) {
      continue;
    }
    if (last != i.first) {
      b.push_back({last, i.first - 1});
    }
    b.push_back({i.first, -1});
    last = i.first + 1;
  }
  int c = 0;
  vector<int> pref(b.size() + 1, 0);
  for (int i = 1; i <= b.size(); ++i) {
    pref[i] = pref[i - 1];
    if (b[i - 1].second == -1) {
      pref[i]++;
    }
  }
  vector<pair<int, int>> d;
  for (auto i : g) {
    if (i.first != i.second) {
      d.push_back(i);
    }
  }
  for (auto i : d) {
    int l = 0, r = b.size();
    int need_l = min(i.first, i.second), need_r = max(i.first, i.second), li,
        ri;
    while (r - l > 1) {
      int m = (r + l) / 2;
      if (need_l >= b[m].first) {
        l = m;
      } else {
        r = m;
      }
    }
    li = l;
    l = 0, r = b.size();
    while (r - l > 1) {
      int m = (r + l) / 2;
      if (need_r >= b[m].first) {
        l = m;
      } else {
        r = m;
      }
    }
    ri = l;
    int cur = max(0, pref[ri] - pref[li + 1]);
    ans += (need_r - need_l - 1 - cur);
    ++c;
  }
  cout << ans;
  return 0;
}
