#include <bits/stdc++.h>
using namespace std;
const long long _ = 4e5 + 100;
inline long long read() {
  char c = getchar();
  long long sign = 1;
  long long x = 0;
  while (c > '9' || c < '0') {
    if (c == '-') sign = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x *= 10;
    x += c - '0';
    c = getchar();
  }
  return x * sign;
}
long long n;
long long A[_];
long long Min[_];
long long Max[_];
namespace SegmentTree {
const long long _ = 3e6 + 100;
struct Node {
  long long MIN;
  long long sMIN;
  long long tar;
  Node operator+(const Node &rhs) const {
    Node res;
    res.MIN = min(MIN, rhs.MIN);
    res.sMIN =
        MIN == rhs.MIN ? (sMIN + rhs.sMIN) : (MIN < rhs.MIN ? sMIN : rhs.sMIN);
    return res;
  }
} v[_];
long long tot = 0;
long long ch[_][2];
long long Groot() {
  return (tot++, ch[tot][0] = ch[tot][1] = v[tot].MIN = v[tot].sMIN = 0, tot);
}
void maintain(long long o) {
  v[o].MIN = min(v[(ch[o][0])].MIN, v[(ch[o][1])].MIN);
  v[o].sMIN =
      v[(ch[o][0])].MIN == v[(ch[o][1])].MIN
          ? v[(ch[o][0])].sMIN + v[(ch[o][1])].sMIN
          : (v[(ch[o][0])].MIN < v[(ch[o][1])].MIN ? v[(ch[o][0])].sMIN
                                                   : v[(ch[o][1])].sMIN);
}
void tar(long long o, long long x) {
  v[o].MIN += x;
  v[o].tar += x;
}
void pushdown(long long o) {
  if (v[o].tar) {
    tar((ch[o][0]), v[o].tar);
    tar((ch[o][1]), v[o].tar);
    v[o].tar = 0;
  }
}
void build(long long o, long long L, long long R) {
  if (L == R) {
    v[o].MIN = 0;
    v[o].sMIN = 1;
    return;
  }
  long long mid = (L + R) >> 1;
  (ch[o][0]) =
      (tot++, ch[tot][0] = ch[tot][1] = v[tot].MIN = v[tot].sMIN = 0, tot);
  (ch[o][1]) =
      (tot++, ch[tot][0] = ch[tot][1] = v[tot].MIN = v[tot].sMIN = 0, tot);
  build((ch[o][0]), L, mid);
  build((ch[o][1]), mid + 1, R);
  maintain(o);
}
void update(long long o, long long nowl, long long nowr, long long L,
            long long R, long long x) {
  if (L > R) return;
  if (L <= nowl && nowr <= R) return tar(o, x);
  long long mid = (nowl + nowr) >> 1;
  pushdown(o);
  if (L <= mid) update((ch[o][0]), nowl, mid, L, R, x);
  if (R > mid) update((ch[o][1]), mid + 1, nowr, L, R, x);
  maintain(o);
}
Node query(long long o, long long nowl, long long nowr, long long L,
           long long R) {
  if (L <= nowl && nowr <= R) return v[o];
  long long mid = (nowl + nowr) >> 1;
  pushdown(o);
  Node Ans;
  Ans.MIN = INT_MAX;
  if (L <= mid) Ans = Ans + query((ch[o][0]), nowl, mid, L, R);
  if (R > mid) Ans = Ans + query((ch[o][1]), mid + 1, nowr, L, R);
  return Ans;
}
long long query(long long o, long long L, long long R) {
  Node res = query(o, 1, n, L, R);
  return res.MIN == 0 ? res.sMIN : 0;
}
}  // namespace SegmentTree
using SegmentTree::build;
using SegmentTree::Groot;
using SegmentTree::query;
using SegmentTree::update;
namespace Mon_Stack {
long long t0t = 0, t1t = 0;
pair<long long, long long> S0[_], S1[_];
long long work(long long *A, long long n) {
  long long Ans = 0;
  long long root = Groot();
  build(root, 1, n);
  S0[0].first = S1[0].first = 0;
  for (long long i = 1; i <= n; i++) {
    update(root, 1, n, 1, i - 1, -1);
    while (t0t != 0 && S0[t0t].second >= A[i]) {
      update(root, 1, n, S0[t0t - 1].first + 1, S0[t0t].first, S0[t0t].second);
      t0t--;
    }
    while (t1t != 0 && S1[t1t].second <= A[i]) {
      update(root, 1, n, S1[t1t - 1].first + 1, S1[t1t].first, -S1[t1t].second);
      t1t--;
    }
    update(root, 1, n, S0[t0t].first + 1, i, -A[i]);
    update(root, 1, n, S1[t1t].first + 1, i, A[i]);
    long long t = query(root, 1, i);
    Ans += t;
    S0[++t0t] = make_pair(i, A[i]);
    S1[++t1t] = make_pair(i, A[i]);
  }
  return Ans;
}
}  // namespace Mon_Stack
using Mon_Stack::work;
signed main() {
  n = read();
  for (long long i = 1; i <= n; i++) {
    long long x = read(), y = read();
    A[x] = y;
  }
  printf("%lld\n", work(A, n));
  return 0;
}
