#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
const int inf = INT_MAX;
inline int read() {
  register int x = 0;
  register char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
  return x;
}
struct each {
  int nu, val;
  each(int x = 0, int y = 0) { val = x, nu = y; }
  each operator+(each k) {
    if (k.val == val) return each(val, k.nu + nu);
    if (k.val < val) return k;
    return each(val, nu);
  }
} b[N];
struct staic {
  int l, r, val;
  staic(int ll = 0, int rr = 0, int x = 0) { l = ll, r = rr, val = x; }
} s1[N], s2[N];
int n, k, t1, t2, a[N], lazy[N];
void get(int rt, int z) { b[rt].val += z, lazy[rt] += z; }
void pushdown(int rt) {
  get(rt << 1 | 1, lazy[rt]);
  get(rt << 1, lazy[rt]), lazy[rt] = 0;
}
void add(int l, int r, int x, int y, int z, int rt) {
  if (x <= l && y >= r) {
    get(rt, z);
    return;
  }
  int mid = (l + r) >> 1;
  if (lazy[rt]) pushdown(rt);
  if (x <= mid) add(l, mid, x, y, z, rt << 1);
  if (y > mid) add(++mid, r, x, y, z, rt << 1 | 1);
  b[rt] = b[rt << 1] + b[rt << 1 | 1];
}
each query(int l, int r, int x, int y, int rt) {
  if (x <= l && y >= r) return b[rt];
  int mid = (l + r) >> 1;
  each ans = each(inf, 0);
  if (lazy[rt]) pushdown(rt);
  if (x <= mid) ans = query(l, mid, x, y, rt << 1);
  if (y > mid) ans = ans + query(++mid, r, x, y, rt << 1 | 1);
  return ans;
}
void init(int l, int r, int rt) {
  if (l == r) {
    b[rt] = each(l, 1);
    return;
  }
  int mid = (l + r) >> 1;
  init(l, mid, rt << 1);
  init(++mid, r, rt << 1 | 1);
  b[rt] = b[rt << 1] + b[rt << 1 | 1];
}
long long ans;
int main() {
  n = read(), init(1, n, 1);
  for (int i = 1; i <= n; i++) a[read()] = read();
  for (int i = 1; i <= n; i++) {
    int l = i;
    while (t1 && s1[t1].val > a[i]) {
      add(1, n, s1[t1].l, s1[t1].r, s1[t1].val, 1);
      l = s1[t1--].l;
    }
    add(1, n, l, i, -a[i], 1);
    s1[++t1] = staic(l, i, a[i]), l = i;
    while (t2 && s2[t2].val < a[i]) {
      add(1, n, s2[t2].l, s2[t2].r, -s2[t2].val, 1);
      l = s2[t2--].l;
    }
    add(1, n, l, i, a[i], 1);
    s2[++t2] = staic(l, i, a[i]);
    ans += query(1, n, 1, i, 1).nu;
  }
  printf("%lld\n", ans);
  return 0;
}
