#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int n, r[maxn], c[maxn], p[maxn], mn[maxn], mx[maxn], cnt[maxn << 1];
long long solve(int l, int r) {
  if (l > r) return 0;
  int m = (l + r) >> 1;
  mn[m] = mx[m] = p[m];
  mn[m + 1] = mx[m + 1] = p[m + 1];
  for (int i = m - 1; i >= l; i--) {
    mn[i] = min(p[i], mn[i + 1]);
    mx[i] = max(p[i], mx[i + 1]);
  }
  for (int i = m + 2; i <= r; i++) {
    mn[i] = min(p[i], mn[i - 1]);
    mx[i] = max(p[i], mx[i - 1]);
  }
  long long ans = 0;
  for (int i = l; i <= m; i++) {
    int tmp = mx[i] - mn[i];
    if (i + tmp >= m && i + tmp <= r && mn[i + tmp] >= mn[i] &&
        mx[i + tmp] <= mx[i])
      ans++;
  }
  for (int i = m + 2; i <= r; i++) {
    int tmp = mx[i] - mn[i];
    if (i - tmp <= m && i - tmp >= l && mn[i - tmp] >= mn[i] &&
        mx[i - tmp] <= mx[i])
      ans++;
  }
  int L = m + 1, R = m + 1;
  for (int i = m; i >= l; i--) {
    while (R <= r && mn[R] >= mn[i]) cnt[R - mx[R] + n]++, R++;
    while (L < R && mx[L] <= mx[i]) cnt[L - mx[L] + n]--, L++;
    ans += cnt[i - mn[i] + n];
  }
  for (int i = L; i < R; i++) {
    cnt[i - mx[i] + n]--;
  }
  L = m, R = m;
  for (int i = m + 1; i <= r; i++) {
    while (R >= l && mn[R] >= mn[i]) cnt[R + mx[R]]++, R--;
    while (L > R && mx[L] <= mx[i]) cnt[L + mx[L]]--, L--;
    ans += cnt[i + mn[i]];
  }
  for (int i = R + 1; i <= L; i++) {
    cnt[i + mx[i]]--;
  }
  return ans + solve(l, m - 1) + solve(m + 1, r);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &r[i], &c[i]), p[r[i]] = c[i];
  }
  printf("%lld\n", solve(1, n));
  return 0;
}
