#include <bits/stdc++.h>
using namespace std;
const int N = (int)3e5 + 7;
int n;
struct T {
  int tree[4 * N], add[4 * N], cnt[4 * N];
  T() {
    memset(add, 0, sizeof add);
    memset(cnt, 0, sizeof cnt);
  }
  void push(int v, int l, int r) {
    if (add[v] == 0) return;
    if (l != r) {
      add[v + v] += add[v];
      add[v + v + 1] += add[v];
    }
    tree[v] += add[v];
    add[v] = 0;
  }
  void gather(int v) {
    tree[v] = min(tree[v + v], tree[v + v + 1]);
    cnt[v] = 0;
    if (tree[v] == tree[v + v]) cnt[v] += cnt[v + v];
    if (tree[v] == tree[v + v + 1]) cnt[v] += cnt[v + v + 1];
  }
  void build(int v = 1, int l = 0, int r = n - 1) {
    if (l == r) {
      tree[v] = l;
      cnt[v] = 1;
      return;
    }
    int mid = (l + r) >> 1;
    build(v + v, l, mid);
    build(v + v + 1, mid + 1, r);
    gather(v);
  }
  void upd(int l, int r, int val, int v = 1, int tl = 0, int tr = n - 1) {
    push(v, tl, tr);
    if (tl > r || tr < l) return;
    if (l <= tl && tr <= r) {
      add[v] += val;
      push(v, tl, tr);
      return;
    }
    int mid = (tl + tr) >> 1;
    upd(l, r, val, v + v, tl, mid);
    upd(l, r, val, v + v + 1, mid + 1, tr);
    gather(v);
  }
};
T tr;
int a[N];
int down[N], up[N];
int main() {
  scanf("%d", &n);
  for (int i = 0, x, y; i < n; i++) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    a[x] = y;
  }
  tr.build();
  down[0] = up[0] = -1;
  int ups, downs;
  ups = downs = 1;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    while (ups >= 2 && a[i] < a[up[ups - 1]]) {
      tr.upd(up[ups - 2] + 1, up[ups - 1], +a[up[ups - 1]]);
      ups--;
    }
    while (downs >= 2 && a[i] > a[down[downs - 1]]) {
      tr.upd(down[downs - 2] + 1, down[downs - 1], -a[down[downs - 1]]);
      downs--;
    }
    up[ups++] = i;
    down[downs++] = i;
    tr.upd(up[ups - 2] + 1, up[ups - 1], -a[up[ups - 1]]);
    tr.upd(down[downs - 2] + 1, down[downs - 1], +a[down[downs - 1]]);
    ans += tr.cnt[1];
  }
  printf("%lld", ans);
}
