#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1e5;
int n, a[Maxn], b[Maxn];
int key[Maxn << 1], cntkey, val[Maxn << 1];
int m;
pair<int, int> s[Maxn << 2];
int bval[Maxn << 2], cntbval;
long long bit[Maxn << 2];
inline int lowbit(int i) { return i & -i; }
long long Sum(int i) {
  long long res = 0;
  for (; i >= 0; i -= lowbit(i + 1)) {
    res += bit[i];
  }
  return res;
}
void Add(int i, long long x) {
  for (; i < cntbval; i += lowbit(i + 1)) {
    bit[i] += x;
  }
}
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", a + i, b + i);
    key[cntkey++] = a[i];
    key[cntkey++] = b[i];
  }
  sort(key, key + cntkey);
  cntkey = unique(key, key + cntkey) - key;
  memcpy(val, key, cntkey * sizeof(int));
  for (int i = 0; i < n; ++i) {
    int a1 = lower_bound(key, key + cntkey, a[i]) - key;
    int b1 = lower_bound(key, key + cntkey, b[i]) - key;
    swap(val[a1], val[b1]);
  }
  for (int i = 0; i < cntkey; ++i) {
    s[m++] = pair<int, int>(val[i], 1);
    bval[cntbval++] = val[i];
    if (i + 1 < cntkey && key[i + 1] - key[i] > 1) {
      s[m++] = pair<int, int>(key[i] + 1, key[i + 1] - key[i] - 1);
      bval[cntbval++] = key[i] + 1;
    }
  }
  sort(bval, bval + cntbval);
  cntbval = unique(bval, bval + cntbval) - bval;
  long long ans = 0;
  for (int i = m - 1; i >= 0; --i) {
    int x = lower_bound(bval, bval + cntbval, s[i].first) - bval;
    ans += s[i].second * Sum(x);
    Add(x, s[i].second);
  }
  cout << ans << endl;
  return 0;
}
