#include <bits/stdc++.h>
using namespace std;
struct Node {
  long long x, y;
} p[330000];
bool cmp(Node a, Node b) { return a.x < b.x; }
long long ans, n;
long long a[330000], maxx[330000], minn[330000], c[330000 << 1];
void solve(long long l, long long r) {
  if (l == r) {
    ans++;
    return;
  }
  long long mid = (l + r) >> 1;
  solve(l, mid);
  solve(mid + 1, r);
  maxx[mid] = minn[mid] = a[mid];
  maxx[mid + 1] = minn[mid + 1] = a[mid + 1];
  for (long long i = mid - 1; i >= l; i--) {
    maxx[i] = max(maxx[i + 1], a[i]);
    minn[i] = min(minn[i + 1], a[i]);
  }
  for (long long i = mid + 2; i <= r; i++) {
    maxx[i] = max(maxx[i - 1], a[i]);
    minn[i] = min(minn[i - 1], a[i]);
  }
  for (long long i = mid; i >= l; i--) {
    long long j = maxx[i] - minn[i] + i;
    if (j <= r && j > mid && maxx[j] < maxx[i] && minn[j] > minn[i]) {
      ans++;
    }
  }
  for (long long i = mid + 1; i <= r; i++) {
    long long j = minn[i] - maxx[i] + i;
    if (j >= l && j <= mid && maxx[j] < maxx[i] && minn[j] > minn[i]) {
      ans++;
    }
  }
  long long j = mid + 1, k = mid + 1;
  for (long long i = mid; i >= l; i--) {
    while (j <= r && minn[j] > minn[i]) {
      c[maxx[j] - j + n]++;
      j++;
    }
    while (k < j && maxx[k] < maxx[i]) {
      c[maxx[k] - k + n]--;
      k++;
    }
    ans += c[minn[i] - i + n];
  }
  while (k < j) {
    c[maxx[k] - k + n]--;
    k++;
  }
  j = mid;
  k = mid;
  for (long long i = mid; i <= r; i++) {
    while (j >= l && minn[j] > minn[i]) {
      c[maxx[j] + j]++;
      j--;
    }
    while (k > j && maxx[k] < maxx[i]) {
      c[maxx[k] + k]--;
      k--;
    }
    ans += c[minn[i] + i];
  }
  while (k > j) {
    c[maxx[k] + k]--;
    k--;
  }
}
int main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d %I64d", &p[i].x, &p[i].y);
  }
  sort(p + 1, p + 1 + n, cmp);
  for (long long i = 1; i <= n; i++) {
    a[i] = p[i].y;
  }
  solve(1, n);
  printf("%I64d", ans);
}
