#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 300005;
struct POS {
  int lin, clm;
  explicit POS(int X = 0, int Y = 0) {
    lin = X;
    clm = Y;
  }
  bool operator<(const POS& b) const { return lin < b.lin; }
};
int n;
POS pos[MAX_N];
int cur_max[MAX_N], cur_min[MAX_N], cnt[MAX_N << 1];
long long ans;
inline int rd() {
  register int x = 0, c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = x * 10 + (c ^ 48);
    c = getchar();
  }
  return x;
}
inline void init_min_max(int l, int mid, int r) {
  cur_max[mid] = 0;
  cur_min[mid] = 0x3f3f3f3f;
  for (int i = mid + 1; i <= r; ++i) {
    cur_max[i] = max(cur_max[i - 1], pos[i].clm);
    cur_min[i] = min(cur_min[i - 1], pos[i].clm);
  }
  cur_max[mid] = cur_min[mid] = pos[mid].clm;
  for (int i = mid - 1; i >= l; --i) {
    cur_max[i] = max(cur_max[i + 1], pos[i].clm);
    cur_min[i] = min(cur_min[i + 1], pos[i].clm);
  }
}
inline void calc_same(int l, int mid, int r) {
  register int dst = 0;
  for (int i = l; i <= mid; ++i) {
    dst = cur_max[i] - cur_min[i] + i;
    ans += (mid < dst && dst <= r && cur_max[dst] < cur_max[i] &&
            cur_min[dst] > cur_min[i]);
  }
  for (int i = mid + 1; i <= r; ++i) {
    dst = i - cur_max[i] + cur_min[i];
    ans += (l <= dst && dst <= mid && cur_max[dst] < cur_max[i] &&
            cur_min[dst] > cur_min[i]);
  }
}
inline void calc_diff(int l, int mid, int r) {
  register int max_ptr = mid + 1, min_ptr = mid + 1;
  for (int i = mid; i >= l; --i) {
    while (max_ptr <= r && cur_max[max_ptr] <= cur_max[i]) {
      ++cnt[cur_min[max_ptr] + max_ptr];
      ++max_ptr;
    }
    while (min_ptr < max_ptr && cur_min[min_ptr] >= cur_min[i]) {
      --cnt[cur_min[min_ptr] + min_ptr];
      ++min_ptr;
    }
    ans += cnt[cur_max[i] + i];
  }
  while (min_ptr < max_ptr) {
    --cnt[cur_min[min_ptr] + min_ptr];
    ++min_ptr;
  }
  max_ptr = mid;
  min_ptr = mid;
  for (int i = mid + 1; i <= r; ++i) {
    while (max_ptr >= l && cur_max[max_ptr] <= cur_max[i]) {
      ++cnt[cur_min[max_ptr] - max_ptr + n];
      --max_ptr;
    }
    while (min_ptr > max_ptr && cur_min[min_ptr] >= cur_min[i]) {
      --cnt[cur_min[min_ptr] - min_ptr + n];
      --min_ptr;
    }
    ans += cnt[cur_max[i] - i + n];
  }
  while (min_ptr > max_ptr) {
    --cnt[cur_min[min_ptr] - min_ptr + n];
    --min_ptr;
  }
}
inline void dvd(int l, int r) {
  int mid = ((l + r) >> 1);
  if (l == r) {
    ++ans;
    return;
  }
  dvd(l, mid);
  dvd(mid + 1, r);
  init_min_max(l, mid, r);
  calc_same(l, mid, r);
  calc_diff(l, mid, r);
}
int main() {
  n = rd();
  for (int i = 1; i <= n; ++i) {
    pos[i].lin = rd();
    pos[i].clm = rd();
  }
  sort(pos + 1, pos + n + 1);
  dvd(1, n);
  printf("%lld\n", ans);
  return 0;
}
