#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
  return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
  return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
  if (s == t) {
    t = (s = buf) + fread(buf, 1, bufl, stdin);
    if (s == t) return EOF;
  }
  return *s++;
}
inline int ty() {
  int a = 0;
  int b = 1, c = fetch();
  while (!isdigit(c)) b ^= c == '-', c = fetch();
  while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
  return b ? a : -a;
}
}  // namespace ae86
using ae86::ty;
const int _ = 300007, inf = 0x3f3f3f3f;
struct cxk {
  int v, x;
  cxk(int v_ = inf, int x_ = 0) { v = v_, x = x_; }
  friend cxk operator+(cxk a, cxk b) {
    cxk c;
    c.v = min(a.v, b.v), c.x = 0;
    if (c.v == a.v) c.x += a.x;
    if (c.v == b.v) c.x += b.x;
    return c;
  }
};
struct sgt {
  static const int _t = _ << 2;
  cxk t[_t];
  int tadd[_t];
  void up(int x) { t[x] = t[(x << 1)] + t[(x << 1 | 1)]; }
  void putadd(int x, int d) { t[x].v += d, tadd[x] += d; }
  void dwn(int x) {
    if (tadd[x])
      putadd((x << 1), tadd[x]), putadd((x << 1 | 1), tadd[x]), tadd[x] = 0;
  }
  void plant(int x, int l, int r) {
    t[x] = cxk(0, r - l + 1), tadd[x] = 0;
    if (l == r) return;
    plant((x << 1), l, ((l + r) >> 1)),
        plant((x << 1 | 1), ((l + r) >> 1) + 1, r), up(x);
  }
  void change(int x, int l, int r, int tl, int tr, int dlt) {
    if (tl <= l && r <= tr) {
      putadd(x, dlt);
      return;
    }
    dwn(x);
    if (tl <= ((l + r) >> 1)) change((x << 1), l, ((l + r) >> 1), tl, tr, dlt);
    if (tr > ((l + r) >> 1))
      change((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr, dlt);
    up(x);
  }
  cxk finder(int x, int l, int r, int tl, int tr) {
    if (tl <= l && r <= tr) return t[x];
    dwn(x);
    if (tr <= ((l + r) >> 1))
      return finder((x << 1), l, ((l + r) >> 1), tl, tr);
    if (tl > ((l + r) >> 1))
      return finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr);
    return finder((x << 1), l, ((l + r) >> 1), tl, tr) +
           finder((x << 1 | 1), ((l + r) >> 1) + 1, r, tl, tr);
  }
} t;
int n, val[_], las[_] = {0};
int stkx[_] = {0}, topx = 0, stki[_] = {0}, topi = 0;
int main() {
  ios::sync_with_stdio(0), cout.tie(nullptr);
  n = ty();
  for (int i = 1, a, b; i <= n; i++) a = ty(), b = ty(), val[a] = b;
  t.plant(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    while (topx && val[stkx[topx]] < val[i])
      t.change(1, 1, n, stkx[topx - 1] + 1, stkx[topx], -val[stkx[topx]]),
          topx--;
    while (topi && val[stki[topi]] > val[i])
      t.change(1, 1, n, stki[topi - 1] + 1, stki[topi], val[stki[topi]]),
          topi--;
    t.change(1, 1, n, las[val[i]] + 1, i, -1), las[val[i]] = i;
    t.change(1, 1, n, stkx[topx] + 1, i, val[i]),
        t.change(1, 1, n, stki[topi] + 1, i, -val[i]);
    stkx[++topx] = i, stki[++topi] = i;
    ans += t.finder(1, 1, n, 1, n).x;
  }
  cout << ans << lf;
  return 0;
}
