#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 3e5 + 50;
stack<long long> s1, s2;
long long a[MAXN];
long long N;
struct node {
  long long x, y;
} data[MAXN];
struct BIT {
  long long minn[MAXN * 4], cnt[MAXN * 4], tag[MAXN * 4];
  void pushdown(long long x) {
    minn[(x << 1)] += tag[x];
    minn[((x << 1) | 1)] += tag[x];
    tag[(x << 1)] += tag[x];
    tag[((x << 1) | 1)] += tag[x];
    tag[x] = 0;
  }
  void pushup(long long x) {
    minn[x] = min(minn[(x << 1)], minn[((x << 1) | 1)]);
    cnt[x] = (minn[(x << 1)] == minn[x]) * cnt[(x << 1)] +
             (minn[((x << 1) | 1)] == minn[x]) * cnt[((x << 1) | 1)];
  }
  void build(long long x, long long l, long long r) {
    if (l == r) {
      minn[x] = l;
      cnt[x] = 1;
      return;
    }
    long long mid = (l + r) >> 1;
    build((x << 1), l, mid);
    build(((x << 1) | 1), mid + 1, r);
    pushup(x);
  }
  void modify(long long x, long long l, long long r, long long tl, long long tr,
              long long v) {
    if (tl > r || tr < l) return;
    if (tl <= l && tr >= r) {
      minn[x] += v;
      tag[x] += v;
      return;
    }
    pushdown(x);
    long long mid = (l + r) >> 1;
    modify((x << 1), l, mid, tl, tr, v);
    modify(((x << 1) | 1), mid + 1, r, tl, tr, v);
    pushup(x);
  }
  long long query(long long x, long long l, long long r, long long tl,
                  long long tr) {
    if (tl > r || tr < l) return 0;
    if (tl <= l && tr >= r) return minn[x] ? 0 : cnt[x];
    pushdown(x);
    long long mid = (l + r) >> 1;
    return query((x << 1), l, mid, tl, tr) +
           query(((x << 1) | 1), mid + 1, r, tl, tr);
  }
} T;
bool cmp(node a, node b) { return a.x < b.x; }
signed main() {
  scanf("%I64d", &N);
  for (long long i = 1; i <= N; ++i)
    scanf("%I64d%I64d", &data[i].x, &data[i].y);
  sort(data + 1, data + N + 1, cmp);
  T.build(1, 1, N);
  long long ans = 0;
  for (long long i = 1; i <= N; ++i) {
    a[i] = data[i].y;
    T.modify(1, 1, N, 1, N, -1);
    while (!s1.empty() && a[i] > a[s1.top()]) {
      long long tmp = s1.top();
      s1.pop();
      if (!s1.empty())
        T.modify(1, 1, N, s1.top() + 1, tmp, -a[tmp]);
      else
        T.modify(1, 1, N, 1, tmp, -a[tmp]);
    }
    if (!s1.empty())
      T.modify(1, 1, N, s1.top() + 1, i, a[i]);
    else
      T.modify(1, 1, N, 1, i, a[i]);
    s1.push(i);
    while (!s2.empty() && a[i] < a[s2.top()]) {
      long long tmp = s2.top();
      s2.pop();
      if (!s2.empty())
        T.modify(1, 1, N, s2.top() + 1, tmp, a[tmp]);
      else
        T.modify(1, 1, N, 1, tmp, a[tmp]);
    }
    if (!s2.empty())
      T.modify(1, 1, N, s2.top() + 1, i, -a[i]);
    else
      T.modify(1, 1, N, 1, i, -a[i]);
    s2.push(i);
    ans += T.query(1, 1, N, 1, i);
  }
  printf("%I64d\n", ans);
  return 0;
}
