#include <bits/stdc++.h>
int n;
int perm[300003];
int min[300003], max[300003];
int cnt[600003];
long long int ans = 0;
void solve(int l, int r) {
  if (r - l == 1) {
    ++ans;
    return;
  }
  int mid = (l + r) >> 1;
  solve(l, mid);
  solve(mid, r);
  min[mid - 1] = perm[mid - 1];
  for (int i = mid - 2; i >= l; --i) min[i] = std::min(min[i + 1], perm[i]);
  max[mid - 1] = perm[mid - 1];
  for (int i = mid - 2; i >= l; --i) max[i] = std::max(max[i + 1], perm[i]);
  min[mid] = perm[mid];
  for (int i = mid + 1; i < r; ++i) min[i] = std::min(min[i - 1], perm[i]);
  max[mid] = perm[mid];
  for (int i = mid + 1; i < r; ++i) max[i] = std::max(max[i - 1], perm[i]);
  long long int val = 0;
  for (int i = mid - 1; i >= l; --i) {
    int k = max[i] - min[i] + i;
    if (k >= mid && k < r && max[k] < max[i] && min[k] > min[i]) ++val;
  }
  for (int i = mid; i < r; ++i) {
    int k = i - max[i] + min[i];
    if (k >= l && k < mid && max[k] < max[i] && min[k] > min[i]) ++val;
  }
  int a = mid, b = mid;
  for (int i = mid - 1; i >= l; --i) {
    while (b < r && max[b] < max[i]) {
      ++cnt[min[b] + b];
      ++b;
    }
    while (a < b && min[a] > min[i]) {
      --cnt[min[a] + a];
      ++a;
    }
    val += cnt[max[i] + i];
  }
  while (a < b) {
    --cnt[min[a] + a];
    ++a;
  }
  a = mid - 1, b = mid - 1;
  for (int i = mid; i < r; ++i) {
    while (a >= l && max[a] < max[i]) {
      ++cnt[min[a] - a + n];
      --a;
    }
    while (b > a && min[b] > min[i]) {
      --cnt[min[b] - b + n];
      --b;
    }
    val += cnt[max[i] - i + n];
  }
  while (b > a) {
    --cnt[min[b] - b + n];
    --b;
  }
  ans += val;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    perm[a - 1] = b - 1;
  }
  solve(0, n);
  printf("%I64d\n", ans);
}
