#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "\n";
  err(++it, args...);
}
function<void(void)> ____ = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
};
template <typename T>
vector<T> &operator<<(vector<T> &__container, T x) {
  __container.push_back(x);
  return __container;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &__container) {
  for (T _ : __container) out << _ << ' ';
  return out;
}
void sci() {}
template <typename... Args>
void sci(int &x, Args &...args) {
  scanf("%d", &x);
  sci(args...);
}
void scl() {}
template <typename... Args>
void scl(long long int &x, Args &...args) {
  scanf("%lld", &x);
  scl(args...);
}
const int MAXN = 3e5 + 7;
struct SegmentTree {
  int l[MAXN << 2], r[MAXN << 2], lazy[MAXN << 2], minn[MAXN << 2],
      cnt[MAXN << 2];
  void pushup(int rt) {
    if (minn[rt << 1] < minn[rt << 1 | 1])
      minn[rt] = minn[rt << 1], cnt[rt] = cnt[rt << 1];
    if (minn[rt << 1] > minn[rt << 1 | 1])
      minn[rt] = minn[rt << 1 | 1], cnt[rt] = cnt[rt << 1 | 1];
    if (minn[rt << 1] == minn[rt << 1 | 1])
      minn[rt] = minn[rt << 1], cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
  }
  void pushdown(int rt) {
    if (!lazy[rt]) return;
    lazy[rt << 1] += lazy[rt];
    lazy[rt << 1 | 1] += lazy[rt];
    minn[rt << 1] += lazy[rt];
    minn[rt << 1 | 1] += lazy[rt];
    lazy[rt] = 0;
  }
  void build(int L, int R, int rt = 1) {
    l[rt] = L;
    r[rt] = R;
    if (l[rt] + 1 == r[rt]) {
      cnt[rt] = 1;
      return;
    }
    int mid = (l[rt] + r[rt]) >> 1;
    build(L, mid, rt << 1);
    build(mid, R, rt << 1 | 1);
    pushup(rt);
  }
  void modify(int L, int R, int x, int rt = 1) {
    if (L >= r[rt] or l[rt] >= R) return;
    if (L <= l[rt] and r[rt] <= R) {
      lazy[rt] += x;
      minn[rt] += x;
      return;
    }
    pushdown(rt);
    modify(L, R, x, rt << 1);
    modify(L, R, x, rt << 1 | 1);
    pushup(rt);
  }
  pair<int, int> query(int L, int R, int rt = 1) {
    if (l[rt] >= R or L >= r[rt]) return make_pair(0x3f3f3f3f, 0);
    if (L <= l[rt] and r[rt] <= R) return make_pair(minn[rt], cnt[rt]);
    pushdown(rt);
    auto p1 = query(L, R, rt << 1);
    auto p2 = query(L, R, rt << 1 | 1);
    if (p1.first == p2.first) return make_pair(p1.first, p1.second + p2.second);
    if (p1.first < p2.first) return p1;
    if (p1.first > p2.first) return p2;
  }
} ST;
int n, A[MAXN];
int minlim[MAXN], maxlim[MAXN];
void solve() {
  sci(n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    sci(x, y);
    A[x] = y;
  }
  ST.build(1, n + 1);
  long long int ret = 0;
  int scanline = 0;
  stack<int> stkmin, stkmax;
  while (++scanline <= n) {
    int &x = scanline;
    minlim[x] = maxlim[x] = x;
    while (!stkmax.empty() and A[stkmax.top()] < A[x]) {
      ST.modify(maxlim[stkmax.top()], maxlim[x], A[x] - A[stkmax.top()]);
      maxlim[x] = maxlim[stkmax.top()];
      stkmax.pop();
    }
    stkmax.push(x);
    while (!stkmin.empty() and A[stkmin.top()] > A[x]) {
      ST.modify(minlim[stkmin.top()], minlim[x], A[stkmin.top()] - A[x]);
      minlim[x] = minlim[stkmin.top()];
      stkmin.pop();
    }
    stkmin.push(x);
    ret += ST.query(1, x + 1).second;
    ST.modify(1, x + 1, -1);
  }
  cout << ret << "\n";
}
int main() {
  solve();
  return 0;
}
