#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 5;
const long long maxm = 1e3 + 5;
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
  return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
          (iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template <class T1, class... T2>
inline void qread(T1 &IEE, T2 &...ls) {
  register T1 __ = 0, ___ = 1;
  register char ch;
  while (!isdigit(ch = gc())) ___ = (ch == '-') ? -___ : ___;
  do {
    __ = (__ << 1) + (__ << 3) + (ch ^ 48);
  } while (isdigit(ch = gc()));
  __ *= ___;
  IEE = __;
  qread(ls...);
  return;
}
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
  return;
}
inline void putc_(char _x) {
  *oS++ = _x;
  if (oS == oT) flush();
}
inline void qwrite() {}
template <class T1, class... T2>
inline void qwrite(T1 IEE, T2... ls) {
  if (!IEE) putc_('0');
  if (IEE < 0) putc_('-'), IEE = -IEE;
  while (IEE) _st[++_qr] = IEE % 10 + '0', IEE /= 10;
  while (_qr) putc_(_st[_qr--]);
  qwrite(ls...);
  return;
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher;
}  // namespace IO
using namespace IO;
long long n, a[maxn], x, y, ans;
long long ma[maxn], mi[maxn], cnt1[maxn << 1];
long long cnt2[maxn << 2], cnt5[maxn << 1];
long long cnt3[maxn << 2], cnt4[maxn << 2];
void merge(long long l, long long r) {
  if (l == r) {
    ans++;
    return;
  }
  long long mid = (l + r) >> 1;
  merge(l, mid);
  merge(mid + 1, r);
  ma[mid] = 0;
  mi[mid] = 0x3f3f3f3f;
  for (int i = mid + 1; i <= r; i++) {
    ma[i] = max(ma[i - 1], a[i]);
    mi[i] = min(mi[i - 1], a[i]);
    cnt4[mi[i] - ma[i] + i + maxn]++;
  }
  long long pl = mid + 1, pr = mid + 1;
  long long mx = 0, mn = 0x3f3f3f3f;
  for (int i = mid; i >= l; i--) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
    while (pl <= r && a[pl] < mx) {
      cnt1[ma[pl] - pl + maxn]--;
      cnt2[mi[pl] - ma[pl] + pl + maxn]++;
      cnt5[mi[pl] + pl]++;
      pl++;
    }
    while (pr <= r && a[pr] > mn) {
      cnt1[ma[pr] - pr + maxn]++;
      cnt3[mi[pr] - ma[pr] + pr + maxn]++;
      cnt5[mi[pr] + pr]--;
      pr++;
    }
    if (pl <= pr) {
      if (mx - mn + i > mid && mx - mn + i < pl) {
        ans++;
      }
      ans += cnt1[mn - i + maxn];
      ans += cnt4[i + maxn] - cnt3[i + maxn];
    } else {
      if (mx - mn + i > mid && mx - mn + i < pr) {
        ans++;
      }
      ans += cnt5[mx + i];
      ans += cnt4[i + maxn] - cnt2[i + maxn];
    }
  }
  for (int i = mid + 1; i <= r; i++) {
    cnt1[ma[i] - i + maxn] = 0;
    cnt2[mi[i] - ma[i] + i + maxn] = cnt3[mi[i] - ma[i] + i + maxn] =
        cnt4[mi[i] - ma[i] + i + maxn] = 0;
    cnt5[mi[i] + i] = 0;
  }
  return;
}
int main() {
  qread(n);
  for (int i = 1; i <= n; i++) {
    qread(x, y);
    a[x] = y;
  }
  merge(1, n);
  cout << ans << endl;
  return 0;
}
