#include <bits/stdc++.h>
using namespace std;
struct T {
  int tg, x, gs;
} t[1000005 << 2];
void build(int rt, int l, int r) {
  t[rt].gs = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
}
void add(int rt, int x) { t[rt].tg += x, t[rt].x += x; }
void pushdown(int rt) {
  add(rt << 1, t[rt].tg);
  add(rt << 1 | 1, t[rt].tg);
  t[rt].tg = 0;
}
void update(int rt, int l, int r, int L, int R, int o) {
  if (L <= l && r <= R) {
    add(rt, o);
    return;
  }
  pushdown(rt);
  int mid = (l + r) >> 1;
  if (L <= mid) update(rt << 1, l, mid, L, R, o);
  if (R > mid) update(rt << 1 | 1, mid + 1, r, L, R, o);
  t[rt].x = min(t[rt << 1].x, t[rt << 1 | 1].x);
  t[rt].gs = (t[rt << 1].x == t[rt].x ? t[rt << 1].gs : 0) +
             (t[rt << 1 | 1].x == t[rt].x ? t[rt << 1 | 1].gs : 0);
}
int n, sta1[1000005], sta2[1000005], top1, top2, a[1000005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x] = y;
  }
  build(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    while (top1 && a[sta1[top1]] < a[i])
      update(1, 1, n, sta1[top1 - 1] + 1, sta1[top1], -a[sta1[top1]]), top1--;
    while (top2 && a[sta2[top2]] > a[i])
      update(1, 1, n, sta2[top2 - 1] + 1, sta2[top2], a[sta2[top2]]), top2--;
    update(1, 1, n, 1, i, -1);
    sta1[++top1] = sta2[++top2] = i;
    update(1, 1, n, sta1[top1 - 1] + 1, i, a[i]),
        update(1, 1, n, sta2[top2 - 1] + 1, i, -a[i]);
    ans += t[1].gs;
  }
  printf("%lld", ans);
  return 0;
}
