#include <bits/stdc++.h>
namespace IO {
char buf[1 << 20 | 1], *f1 = buf, *f2 = buf, pbuf[1 << 24 | 1], *f3 = pbuf,
                       sta[100];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
template <class T>
inline void write(T x) {
  if (!x) return (void)(*f3++ = 48, *f3++ = ' ');
  if (x < 0) *f3++ = '-', x = ~x + 1;
  char *top = sta;
  while (x) *top++ = x % 10 + 48, x /= 10;
  while (top != sta) *f3++ = *--top;
  *f3++ = ' ';
}
inline void put(std::string s) {
  int len = s.length();
  for (int i = 0; i < len; i++) *f3++ = s[i];
  *f3++ = ' ';
}
inline void endline() { *f3++ = '\n'; }
template <class T>
inline T abs(T a) {
  return a < 0 ? -a : a;
}
template <class T>
inline T max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T min(T a, T b) {
  return a < b ? a : b;
}
template <class T>
inline void swap(T &a, T &b) {
  T t = a;
  a = b;
  b = t;
}
}  // namespace IO
using namespace IO;
const int N = 3e5 + 5;
int n, top1, top2;
struct cow {
  int x, y;
  cow(int x = 0, int y = 0) : x(x), y(y) {}
  friend bool operator<(const cow &a, const cow &b) { return a.x < b.x; }
} e[N], st1[N], st2[N];
namespace SMT {
struct node {
  node *ls, *rs;
  int minn, cnt, tag;
  node() {
    ls = rs = NULL;
    minn = cnt = 0;
  }
  inline void up() {
    minn = min(ls->minn, rs->minn);
    cnt = 0;
    if (minn == ls->minn) cnt += ls->cnt;
    if (minn == rs->minn) cnt += rs->cnt;
  }
  inline void add(int k) {
    minn += k;
    tag += k;
  }
  inline void down() {
    if (!tag) return;
    ls->add(tag);
    rs->add(tag);
    tag = 0;
  }
} * root, pool[N << 1], *tail = pool;
void build(node *&o, int l, int r) {
  o = new (tail++) node();
  if (l == r) return (void)(o->minn = l, o->cnt = 1);
  int mid = (l + r) >> 1;
  build(o->ls, l, mid);
  build(o->rs, mid + 1, r);
  o->up();
}
void chenge(node *o, int l, int r, int x, int y, int k) {
  if (x <= l && r <= y) return (void)(o->add(k));
  int mid = (l + r) >> 1;
  o->down();
  if (x <= mid) chenge(o->ls, l, mid, x, y, k);
  if (y > mid) chenge(o->rs, mid + 1, r, x, y, k);
  o->up();
}
int query(node *o, int l, int r, int x, int y) {
  if (x <= l && r <= y) return o->minn == 0 ? o->cnt : 0;
  int mid = (l + r) >> 1;
  o->down();
  return (x <= mid ? query(o->ls, l, mid, x, y) : 0) +
         (y > mid ? query(o->rs, mid + 1, r, x, y) : 0);
}
}  // namespace SMT
using namespace SMT;
int main() {
  n = read();
  long long ans = 0;
  for (int i = 1; i <= n; i++) e[i].x = read(), e[i].y = read();
  std::sort(e + 1, e + 1 + n);
  build(root, 1, n);
  for (int i = 1; i <= n; i++) {
    chenge(root, 1, n, 1, n, -1);
    while (top1 && st1[top1].y < e[i].y)
      chenge(root, 1, n, st1[top1 - 1].x + 1, st1[top1].x,
             e[i].y - st1[top1].y),
          top1--;
    while (top2 && st2[top2].y > e[i].y)
      chenge(root, 1, n, st2[top2 - 1].x + 1, st2[top2].x,
             st2[top2].y - e[i].y),
          top2--;
    st1[++top1] = cow(i, e[i].y);
    st2[++top2] = cow(i, e[i].y);
    ans += query(root, 1, n, 1, i);
  }
  printf("%lld\n", ans);
  fclose(stdin);
  fclose(stdout);
  return 0;
}
