#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long ans;
int n, a[1010101], b[1010101];
struct point {
  int x, y;
} x[1010101];
inline bool cmp(point a, point b) { return a.x < b.x; }
struct Stack {
  int sta[1010101], tp;
  inline int top() { return sta[tp]; }
  inline int setop() { return sta[tp - 1]; }
  inline void pop() { tp--; }
  inline void push(int x) { sta[++tp] = x; }
  inline bool empty() { return (tp == 0); }
  inline int size() { return tp; }
} Smi, Sma;
int mi[2001010], seg[2020202], tag[2020202];
inline void pushup(int rt) {
  if (mi[rt << 1] == mi[rt << 1 | 1]) {
    seg[rt] = seg[rt << 1] + seg[rt << 1 | 1], mi[rt] = mi[rt << 1];
  } else if (mi[rt << 1] < mi[rt << 1 | 1]) {
    seg[rt] = seg[rt << 1], mi[rt] = mi[rt << 1];
  } else {
    seg[rt] = seg[rt << 1 | 1], mi[rt] = mi[rt << 1 | 1];
  }
}
void build(int rt, int lb, int rb) {
  if (lb == rb) return (void)(seg[rt] = 1);
  build(rt << 1, lb, ((lb + rb) >> 1));
  build(rt << 1 | 1, ((lb + rb) >> 1) + 1, rb);
  pushup(rt);
}
int ANS;
inline void pushdown(int rt, int lb, int rb) {
  if (tag[rt]) {
    tag[rt << 1] += tag[rt], tag[rt << 1 | 1] += tag[rt];
    mi[rt << 1] += tag[rt], mi[rt << 1 | 1] += tag[rt];
    tag[rt] = 0;
  }
}
void query(int rt, int lb, int rb, int l, int r) {
  if (lb > r || rb < l) return;
  if (lb >= l && rb <= r) {
    if (mi[rt] == 0) ANS += seg[rt];
    return;
  }
  pushdown(rt, lb, rb);
  query(rt << 1, lb, ((lb + rb) >> 1), l, r),
      query(rt << 1 | 1, ((lb + rb) >> 1) + 1, rb, l, r);
}
void update(int rt, int lb, int rb, int l, int r, int w) {
  if (lb > r || rb < l) return;
  if (lb >= l && rb <= r) return (void)(mi[rt] += w, tag[rt] += w);
  pushdown(rt, lb, rb);
  update(rt << 1, lb, ((lb + rb) >> 1), l, r, w),
      update(rt << 1 | 1, ((lb + rb) >> 1) + 1, rb, l, r, w);
  pushup(rt);
}
void getall(int rt, int lb, int rb) {
  if (lb == rb) return (void)(printf("%d ", mi[rt]));
  pushdown(rt, lb, rb), getall(rt << 1, lb, ((lb + rb) >> 1));
  getall(rt << 1 | 1, ((lb + rb) >> 1) + 1, rb);
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    x[i].x = read(), x[i].y = read();
  }
  sort(x + 1, x + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    b[i] = a[i] = x[i].y;
  }
  Smi.push(0), Sma.push(0);
  b[0] = 0x3f3f3f3f;
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    while (!Smi.empty() && a[Smi.top()] > a[i]) {
      update(1, 1, n, Smi.setop() + 1, Smi.top(), a[Smi.top()] - a[i]),
          Smi.pop();
    }
    Smi.push(i);
    while (!Sma.empty() && b[Sma.top()] < b[i]) {
      update(1, 1, n, Sma.setop() + 1, Sma.top(), b[i] - b[Sma.top()]),
          Sma.pop();
    }
    Sma.push(i);
    if (i - 1) update(1, 1, n, 1, i - 1, -1);
    ANS = 0;
    query(1, 1, n, 1, i);
    ans += ANS;
  }
  cout << ans;
  return 0;
}
