#include <bits/stdc++.h>
using namespace std;
void fastStream() {
  cin.tie(0);
  std::ios_base::sync_with_stdio(0);
}
const int MAX_N = 1000100;
class BIT {
 private:
  long long bit[MAX_N];
  int n;

 public:
  BIT(int sz) {
    n = sz;
    for (int i = 0; i <= n; i++) bit[i] = 0;
  }
  long long sum(int i) {
    long long s = 0;
    while (i > 0) {
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  long long rangeSum(int from, int to) {
    long long res = 0;
    res = (sum(to) - sum(from - 1));
    return res;
  }
  void add(int i, long long x) {
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
  void insert(int i, long long x) { add(i, -(sum(i) - sum(i - 1)) + x); }
};
BIT bit(1000099);
int main() {
  map<int, int> m;
  fastStream();
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int p1, p2;
    if (m.count(a))
      p1 = m[a];
    else
      p1 = a;
    if (m.count(b))
      p2 = m[b];
    else
      p2 = b;
    m[a] = p2;
    m[b] = p1;
  }
  if (m.count(0) == 0) m[0] = 0;
  vector<int> between_index;
  for (pair<int, int> p : m) between_index.push_back(p.first);
  vector<pair<int, int> > indices;
  map<int, int> poses;
  for (int i = 0; i < (int)between_index.size(); i++) {
    if (i > 0) {
      int a = between_index[i - 1];
      int b = between_index[i];
      if (b - a > 1) {
        indices.push_back(make_pair(a + 1, b - a - 1));
      }
    }
    indices.push_back(make_pair(between_index[i], 1));
  }
  for (int i = 0; i < (int)indices.size(); i++) {
    poses[indices[i].first] = i;
  }
  long long res = 0;
  for (int i = 0; i < (int)indices.size(); i++) {
    if (m.count(indices[i].first)) {
      int val = m[indices[i].first];
      int idx = poses[val] + 1;
      res += bit.rangeSum(idx, 1000000);
      bit.add(idx, 1);
    } else {
      int idx = poses[indices[i].first] + 1;
      res += bit.rangeSum(idx, 1000000) * indices[i].second;
      bit.add(idx, indices[i].second);
    }
  }
  cout << res << endl;
  return 0;
}
