#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  return x * f;
}
struct node {
  long long mi, tag, cnt;
} tr[1200010];
struct chess {
  long long x, y;
  bool operator<(const chess &X) const { return x < X.x; }
} c[300010], sta[300010], sti[300010];
long long n, ta, ti;
long long ans;
inline void build(long long i, long long l, long long r) {
  if (l == r) {
    tr[i].mi = l;
    tr[i].cnt = 1;
    return;
  }
  long long mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build(i << 1 | 1, mid + 1, r);
  tr[i] = tr[i << 1];
}
inline void pushdown(long long i) {
  long long ls = i << 1, rs = ls | 1;
  tr[ls].mi += tr[i].tag;
  tr[rs].mi += tr[i].tag;
  tr[ls].tag += tr[i].tag;
  tr[rs].tag += tr[i].tag;
  tr[i].tag = 0;
}
void pushup(long long i) {
  long long ls = i << 1, rs = ls | 1;
  tr[i].mi = min(tr[ls].mi, tr[rs].mi);
  tr[i].cnt = 0;
  if (tr[i].mi == tr[ls].mi) tr[i].cnt += tr[ls].cnt;
  if (tr[i].mi == tr[rs].mi) tr[i].cnt += tr[rs].cnt;
}
void update(long long i, long long l, long long r, long long x, long long y,
            long long val) {
  if (l >= x && r <= y) {
    tr[i].mi += val;
    tr[i].tag += val;
    return;
  }
  pushdown(i);
  long long mid = (l + r) >> 1;
  if (mid >= x) update(i << 1, l, mid, x, y, val);
  if (mid < y) update(i << 1 | 1, mid + 1, r, x, y, val);
  pushup(i);
}
long long query(long long i, long long l, long long r, long long x,
                long long y) {
  if (l >= x && r <= y) return tr[i].mi == 0 ? tr[i].cnt : 0;
  long long mid = (l + r) >> 1, ans = 0;
  pushdown(i);
  if (mid >= x) ans += query(i << 1, l, mid, x, y);
  if (mid < y) ans += query(i << 1 | 1, mid + 1, r, x, y);
  return ans;
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) {
    c[i].x = read();
    c[i].y = read();
  }
  sort(c + 1, c + n + 1);
  build(1, 1, n);
  for (long long i = 1; i <= n; i++) {
    update(1, 1, n, 1, n, -1);
    while (ta && sta[ta].y < c[i].y)
      update(1, 1, n, sta[ta - 1].x + 1, sta[ta].x, c[i].y - sta[ta].y), --ta;
    sta[++ta] = (chess){i, c[i].y};
    while (ti && sti[ti].y > c[i].y)
      update(1, 1, n, sti[ti - 1].x + 1, sti[ti].x, sti[ti].y - c[i].y), --ti;
    sti[++ti] = (chess){i, c[i].y};
    ans += query(1, 1, n, 1, i);
  }
  printf("%lld\n", ans);
  return 0;
}
