#include <bits/stdc++.h>
using namespace std;
long long Ans;
int N, M, T1, T2, A[300005], S[300005], S1[300005], S2[300005];
struct Nod {
  int Min, tot, laz;
  void operator+=(int k) {
    Min += k;
    laz += k;
  }
  Nod operator+(const Nod& b) {
    if (Min < b.Min) return (Nod){Min, tot, 0};
    if (Min > b.Min) return (Nod){b.Min, b.tot, 0};
    return (Nod){Min, tot + b.tot, 0};
  }
} T[300005 << 2];
int IN() {
  int x = 0, ch;
  for (; (ch = getchar()) < '0' || ch > '9';)
    ;
  for (; ch >= '0' && ch <= '9'; (ch = getchar())) (x *= 10) += ch - '0';
  return x;
}
void Up(int x) {
  for (x >>= 1; x; x >>= 1) T[x] = T[x << 1] + T[x << 1 | 1];
}
void Dn(int x) {
  for (int s = 1, d = M >> 1, t; d; (s <<= 1) |= (d & x) > 0, d >>= 1)
    if (t = T[s].laz) T[s << 1] += t, T[s << 1 | 1] += t, T[s].laz = 0;
}
void Add(int x, int y, int k) {
  Dn(x + M - 1);
  Dn(y + M + 1);
  for (int l = x + M - 1, r = y + M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (l & 1 ^ 1) T[l ^ 1] += k;
    if (r & 1) T[r ^ 1] += k;
  }
  Up(x + M - 1);
  Up(y + M + 1);
}
Nod Ask(int x, int y) {
  Dn(x + M);
  Dn(y + M);
  int p = 0, q = 0;
  Nod a, b;
  for (int l = x + M - 1, r = y + M + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (l & 1 ^ 1) p ? a = a + T[l ^ 1] : a = T[l ^ 1], p = 1;
    if (r & 1) q ? b = b + T[r ^ 1] : b = T[r ^ 1], q = 1;
  }
  return p && q ? a + b : (p ? a : b);
}
int main() {
  N = IN();
  for (int i = 1; i <= N; i++) A[IN()] = IN();
  for (M = 1; M <= N + 1; M <<= 1)
    ;
  for (int i = 1; i <= N; i++) T[M + i] = (Nod){i, 1, 0};
  for (int i = M; i; i--) T[i] = T[i << 1] + T[i << 1 | 1];
  for (int i = 1; i <= N; i++) {
    for (; T1 && A[S1[T1]] < A[i]; --T1)
      Add(S1[T1 - 1] + 1, S1[T1], A[i] - A[S1[T1]]);
    for (; T2 && A[S2[T2]] > A[i]; --T2)
      Add(S2[T2 - 1] + 1, S2[T2], A[S2[T2]] - A[i]);
    S1[++T1] = S2[++T2] = i;
    Ans += Ask(1, i).tot;
  }
  printf("%I64d\n", Ans);
}
