#include <bits/stdc++.h>
inline int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - 48;
    c = getchar();
  }
  return flag ? x : -x;
}
int s[300007 << 2], num[300007 << 2], tag[300007 << 2];
void build(int id, int lf, int rf) {
  s[id] = 1;
  if (lf == rf) return;
  int mid = (lf + rf) >> 1;
  build(id << 1, lf, mid);
  build(id << 1 | 1, mid + 1, rf);
  s[id] = s[id << 1] + s[id << 1 | 1];
}
inline int min(int x, int y) { return x < y ? x : y; }
void push(int id, int v) { num[id] += v, tag[id] += v; }
void pushdown(int id) {
  push(id << 1, tag[id]), push(id << 1 | 1, tag[id]), tag[id] = 0;
}
void modify(int id, int lf, int rf, int l, int r, int val) {
  if (l <= lf && rf <= r)
    push(id, val);
  else {
    int mid = (lf + rf) >> 1;
    if (tag[id]) pushdown(id);
    if (l <= mid) modify(id << 1, lf, mid, l, r, val);
    if (r > mid) modify(id << 1 | 1, mid + 1, rf, l, r, val);
    num[id] = min(num[id << 1], num[id << 1 | 1]);
    s[id] = (num[id] == num[id << 1] ? s[id << 1] : 0) +
            (num[id] == num[id << 1 | 1] ? s[id << 1 | 1] : 0);
  }
}
int n, ma[300007], mi[300007], t1 = 0, t2 = 0, a[300007];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[read()] = read();
  build(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    modify(1, 1, n, 1, i, -1);
    while (t1 && a[ma[t1]] < a[i])
      modify(1, 1, n, ma[t1 - 1] + 1, ma[t1], a[i] - a[ma[t1]]), t1--;
    while (t2 && a[mi[t2]] > a[i])
      modify(1, 1, n, mi[t2 - 1] + 1, mi[t2], a[mi[t2]] - a[i]), t2--;
    ma[++t1] = mi[++t2] = i;
    ans += 1LL * s[1];
  }
  printf("%lld", ans);
}
