#include <bits/stdc++.h>
const long long NUM = 6e5;
const long long MAXN = 3e5 + 10;
long long n, ans, a[MAXN], cnt[4 * MAXN];
long long smx[MAXN], smi[MAXN], pmx[MAXN], pmi[MAXN], omx[MAXN], omi[MAXN];
inline long long read() {
  long long X = 0, flag = 0;
  char ch = 0;
  while (!isdigit(ch)) flag |= ch == '-', ch = getchar();
  while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return flag ? -X : X;
}
inline void work(long long L, long long R) {
  if (L == R) ans++;
  if (L >= R) return;
  long long mid = (L + R) >> 1;
  smx[mid + 1] = 0, smi[mid + 1] = n + 1;
  pmx[mid] = 0, pmi[mid] = n + 1;
  for (long long i = mid + 1; i <= R; i++) {
    pmx[i] = std::max(pmx[i - 1], a[i]);
    pmi[i] = std::min(pmi[i - 1], a[i]);
  }
  for (long long i = mid; i >= L; i--) {
    smx[i] = std::max(smx[i + 1], a[i]);
    smi[i] = std::min(smi[i + 1], a[i]);
  }
  long long mi = mid, mx = mid;
  omx[mid] = omi[mid] = mid;
  for (long long i = mid + 1; i <= R; i++) {
    while (mx >= L && smx[mx] <= pmx[i]) mx--;
    while (mi >= L && smi[mi] >= pmi[i]) mi--;
    omx[i] = mx, omi[i] = mi;
  }
  long long tot = 0;
  for (long long i = mid + 1; i <= R; i++) {
    long long loc = i + pmi[i] - pmx[i];
    if (omi[i] < loc && omx[i] < loc && loc <= mid) tot++;
  }
  for (long long i = mid + 1; i <= R; i++) cnt[NUM + i - pmx[i]] = 0;
  for (long long i = mid + 1; i <= R; i++) {
    for (long long j = omx[i - 1]; j > omx[i]; j--) cnt[NUM + j - smi[j]]++;
    for (long long j = omi[i - 1]; j > omi[i]; j--) cnt[NUM + j - smi[j]]--;
    if (omx[i] < omi[i]) tot += cnt[NUM + i - pmx[i]];
  }
  for (long long i = mid + 1; i <= R; i++) cnt[NUM + i + pmi[i]] = 0;
  for (long long i = mid + 1; i <= R; i++) {
    for (long long j = omx[i - 1]; j > omx[i]; j--) cnt[NUM + j + smx[j]]--;
    for (long long j = omi[i - 1]; j > omi[i]; j--) cnt[NUM + j + smx[j]]++;
    if (omi[i] < omx[i]) tot += cnt[NUM + i + pmi[i]];
  }
  for (long long i = mid + 1; i <= R; i++) cnt[NUM + i] = 0;
  for (long long i = L; i <= mid; i++) cnt[NUM + smx[i] - smi[i] + i]++;
  long long zq = mid;
  for (long long i = mid + 1; i <= R; i++) {
    while (zq > omx[i]) {
      cnt[NUM + smx[zq] - smi[zq] + zq]--;
      zq--;
    }
    while (zq > omi[i]) {
      cnt[NUM + smx[zq] - smi[zq] + zq]--;
      zq--;
    }
    tot += cnt[NUM + i];
  }
  ans += tot;
  work(L, mid), work(mid + 1, R);
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) a[read()] = read();
  work(1, n);
  printf("%lld\n", ans);
  return 0;
}
