#include <bits/stdc++.h>
using namespace std;
long long re() {
  long long ret = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') f = (ch == '-') ? -f : f, ch = getchar();
  while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
  return ret * f;
}
long long n;
struct Point {
  long long x, y;
} p[300005];
bool cmp(Point a, Point b) { return a.x < b.x; }
struct Node {
  long long k, num;
} t[2400005];
long long lazy[2400005];
Node push_up(Node a, Node b) {
  if (a.k == b.k) return (Node){a.k, a.num + b.num};
  return (a.k < b.k) ? a : b;
}
void push_down(long long o) {
  if (lazy[o]) {
    lazy[o * 2] += lazy[o], lazy[o * 2 + 1] += lazy[o];
    t[o * 2].k += lazy[o], t[o * 2 + 1].k += lazy[o];
    lazy[o] = 0;
  }
}
void modify(long long o, long long l, long long r, long long x, long long y,
            long long k) {
  if (x > y) return;
  push_down(o);
  if (l == x && r == y) {
    t[o].k += k, lazy[o] += k;
    return;
  }
  long long mid = (l + r) / 2;
  if (y <= mid)
    modify(o * 2, l, mid, x, y, k);
  else if (x > mid)
    modify(o * 2 + 1, mid + 1, r, x, y, k);
  else
    modify(o * 2, l, mid, x, mid, k),
        modify(o * 2 + 1, mid + 1, r, mid + 1, y, k);
  t[o] = push_up(t[o * 2], t[o * 2 + 1]);
}
void init(long long o, long long l, long long r, long long k) {
  if (l == r) {
    t[o].num = 1;
    return;
  }
  long long mid = (l + r) / 2;
  if (k <= mid)
    init(o * 2, l, mid, k);
  else
    init(o * 2 + 1, mid + 1, r, k);
}
Node query(long long o, long long l, long long r, long long x, long long y) {
  push_down(o);
  if (l == x && r == y) return t[o];
  long long mid = (l + r) / 2;
  if (y <= mid)
    return query(o * 2, l, mid, x, y);
  else if (x > mid)
    return query(o * 2 + 1, mid + 1, r, x, y);
  else
    return push_up(query(o * 2, l, mid, x, mid),
                   query(o * 2 + 1, mid + 1, r, mid + 1, y));
}
struct Stack {
  long long k, l;
} s1[300005], s2[300005];
long long t1, t2;
long long ans;
signed main() {
  n = re();
  for (long long i = 1; i <= n; ++i) p[i].x = re(), p[i].y = re();
  sort(p + 1, p + 1 + n, cmp);
  for (long long i = 1; i <= n; ++i) {
    init(1, 1, n, i);
    long long l = i;
    while (t1 && s1[t1].k <= p[i].y) {
      modify(1, 1, n, s1[t1].l, l - 1, p[i].y - s1[t1].k);
      l = s1[t1].l, --t1;
    }
    s1[++t1] = (Stack){p[i].y, l};
    l = i;
    while (t2 && s2[t2].k >= p[i].y) {
      modify(1, 1, n, s2[t2].l, l - 1, s2[t2].k - p[i].y);
      l = s2[t2].l, --t2;
    }
    s2[++t2] = (Stack){p[i].y, l};
    modify(1, 1, n, 1, i - 1, -1);
    ans += query(1, 1, n, 1, i).num;
  }
  printf("%lld\n", ans);
  return 0;
}
