#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("-Ofast", "-funroll-all-loops", "-ffast-math")
using namespace std;
using LL = long long;
constexpr LL mod = 1e9 + 7;
long long ex_gcd(long long a, long long b, long long &x, long long &y) {
  if (!b) {
    x = 1;
    y = 0;
    return a;
  }
  long long ret = ex_gcd(b, a % b, y, x);
  y -= a / b * x;
  return ret;
}
LL mul(LL u, LL v, LL p) {
  return (u * v - LL((long double)u * v / p) * p + p) % p;
}
LL bin(LL x, LL n, LL MOD) {
  LL ret = MOD != 1;
  for (x %= MOD; n; n >>= 1, x = x * x % MOD)
    if (n & 1) ret = ret * x % MOD;
  return ret;
}
LL norm(LL x) { return x >= mod ? (x - mod) : x; }
inline LL get_inv(LL x, LL p) { return bin(x, p - 2, p); }
inline long long get_inv(long long a) {
  long long x, y;
  ex_gcd(a, mod, x, y);
  return norm(x + mod);
}
template <class T>
inline void umin(T &x, T y) {
  x = x > y ? y : x;
}
template <class T>
inline void umax(T &x, T y) {
  x = x < y ? y : x;
}
template <class T>
inline void dec(T &x, T y) {
  x -= y;
  if (x < 0) x += mod;
}
template <class T>
inline void add(T &x, T y) {
  x += y;
  if (x >= mod) x -= mod;
}
template <typename T>
void read(T &x) {
  cin >> x;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
  read(first);
  read(args...);
}
template <class T>
LL ctz(T a) {
  return sizeof(T) > 2 ? __builtin_ctzll(a) : __builtin_ctz(a);
}
template <class T>
inline T gcd(T _a, T _b) {
  T a = abs(_a), b = abs(_b);
  LL shift = ctz(a | b);
  for (b >>= ctz(b), a >>= ctz(a); a; a -= b, a >>= ctz(a))
    if (a < b) swap(a, b);
  return b << shift;
}
const double PI = acos(-1.0);
constexpr LL INF = 0x3f3f3f3f;
constexpr long long linf = 0x3f3f3f3f3f3f3f3f;
constexpr unsigned long long base = 2333, P_1 = 19260817, P_2 = 999998639;
constexpr LL N = 1e6 + 10;
struct SegmentTree {
  LL l[N << 2], r[N << 2], lazy[N << 2], minn[N << 2], cnt[N << 2];
  void pushup(LL 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(LL 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(LL L, LL R, LL rt = 1) {
    l[rt] = L;
    r[rt] = R;
    if (l[rt] + 1 == r[rt]) {
      cnt[rt] = 1;
      return;
    }
    LL mid = (l[rt] + r[rt]) >> 1;
    build(L, mid, rt << 1);
    build(mid, R, rt << 1 | 1);
    pushup(rt);
  }
  void modify(LL L, LL R, LL x, LL 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<LL, LL> query(LL L, LL R, LL rt = 1) {
    if (l[rt] >= R or L >= r[rt]) return make_pair(INF, 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;
LL n, A[N];
LL minlim[N], maxlim[N];
void solve() {
  cin >> n;
  for (LL i = 1, x, y; i <= n; i++) {
    cin >> x >> y;
    A[x] = y;
  }
  ST.build(1, n + 1);
  LL ret = 0;
  LL scanline = 0;
  stack<LL> stkmin, stkmax;
  while (++scanline <= n) {
    LL &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 << '\12';
}
int32_t main() {
  solve();
  return 0;
}
