#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int n, a[300005], maxx[300005], minn[300005], t[300005 * 2];
long long ans;
struct Cur {
  int x, y;
  bool operator<(const Cur &w) const { return x < w.x; }
} cur[300005];
void solve(int l, int r) {
  if (l == r) {
    ans++;
    return;
  }
  int mid = l + r >> 1, i, j, z, z1;
  solve(l, mid);
  solve(mid + 1, r);
  minn[mid] = 300005;
  maxx[mid] = -1;
  for (i = mid + 1; i <= r; i++)
    minn[i] = min(minn[i - 1], a[i]), maxx[i] = max(maxx[i - 1], a[i]);
  minn[mid] = maxx[mid] = a[mid];
  for (i = mid - 1; i >= l; i--)
    minn[i] = min(minn[i + 1], a[i]), maxx[i] = max(maxx[i + 1], a[i]);
  i = mid;
  while (i >= l) {
    j = maxx[i] - minn[i] + i;
    if (mid + 1 <= j && j <= r && maxx[j] < maxx[i] && minn[j] > minn[i]) ans++;
    i--;
  }
  j = mid + 1;
  while (j <= r) {
    i = j + minn[j] - maxx[j];
    if (l <= i && i <= mid && maxx[i] < maxx[j] && minn[i] > minn[j]) ans++;
    j++;
  }
  i = mid;
  z = z1 = mid + 1;
  while (i >= l) {
    while (maxx[z] < maxx[i] && z <= r) t[z - maxx[z] + 300005]--, z++;
    while (minn[z1] > minn[i] && z1 <= r) t[z1 - maxx[z1] + 300005]++, z1++;
    if (z < z1) ans += t[i - minn[i] + 300005];
    i--;
  }
  for (i = mid; i <= r; i++) t[i - maxx[i] + 300005] = 0;
  i = mid;
  z = z1 = mid + 1;
  while (i >= l) {
    while (minn[z] > minn[i] && z <= r) t[minn[z] + z]--, z++;
    while (maxx[z1] < maxx[i] && z1 <= r) t[minn[z1] + z1]++, z1++;
    if (z < z1) ans += t[maxx[i] + i];
    i--;
  }
  for (i = mid; i <= r; i++) t[minn[i] + i] = 0;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) cur[i] = (Cur){read(), read()};
  sort(cur + 1, cur + n + 1);
  for (int i = 1; i <= n; i++) a[i] = cur[i].y;
  solve(1, n);
  cout << ans;
}
