#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &x) {
  char ch = getchar();
  int f = 1;
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
int n, a[300050], pos[300050], lzy[300050 << 2], minx[300050 << 2],
    cnt[300050 << 2];
long long ans;
void update(int u) {
  minx[u] = min(minx[u << 1], minx[u << 1 | 1]);
  cnt[u] = cnt[u << 1] * (minx[u] == minx[u << 1]) +
           cnt[u << 1 | 1] * (minx[u] == minx[u << 1 | 1]);
}
void build(int u, int l, int r) {
  if (l == r) return (void)(minx[u] = 1e9, cnt[u] = 1);
  build(u << 1, l, ((l + r) >> 1)), build(u << 1 | 1, ((l + r) >> 1) + 1, r);
  update(u);
}
void put_add(int u, int w) { minx[u] += w, lzy[u] += w; }
void push(int u) {
  if (lzy[u]) put_add(u << 1, lzy[u]), put_add(u << 1 | 1, lzy[u]), lzy[u] = 0;
}
void modify(int u, int l, int r, int x) {
  if (l == r) return (void)(minx[u] = 0, cnt[u] = 1);
  push(u);
  if (x <= ((l + r) >> 1))
    modify(u << 1, l, ((l + r) >> 1), x);
  else
    modify(u << 1 | 1, ((l + r) >> 1) + 1, r, x);
  update(u);
}
void add(int u, int l, int r, int k, int w) {
  if (r <= k) return put_add(u, w);
  if (l > k) return;
  push(u);
  add(u << 1, l, ((l + r) >> 1), k, w),
      add(u << 1 | 1, ((l + r) >> 1) + 1, r, k, w);
  update(u);
}
int main() {
  int x, y;
  read(n);
  for (register int i = 1; i <= n; ++i) read(x), read(y), a[x] = y, pos[y] = x;
  build(1, 1, n);
  for (register int i = 1; i <= n; ++i) {
    modify(1, 1, n, i), add(1, 1, n, i, 1);
    x = a[pos[i] - 1], y = a[pos[i] + 1];
    if (x && x < i) add(1, 1, n, x, -1);
    if (y && y < i) add(1, 1, n, y, -1);
    ans += cnt[1];
  }
  printf("%lld", ans);
  return 0;
}
