#include <bits/stdc++.h>
using namespace std;
const int maxn = 500007;
int i, j, k, l, t, n, m;
long long ans;
int a[maxn], da[maxn], xiao[maxn], tong[maxn];
void solve(int l, int r) {
  if (l > r) return;
  int mid = (l + r) / 2, i, j, k, t;
  da[mid] = xiao[mid] = a[mid];
  da[mid + 1] = xiao[mid + 1] = a[mid + 1];
  for (i = mid - 1; i >= l; i--)
    da[i] = max(da[i + 1], a[i]), xiao[i] = min(xiao[i + 1], a[i]);
  for (i = mid + 2; i <= r; i++)
    da[i] = max(da[i - 1], a[i]), xiao[i] = min(xiao[i - 1], a[i]);
  for (j = mid; j >= l; j--) {
    i = j + (da[j] - xiao[j]);
    if (i >= mid && i <= r && da[j] >= da[i] && xiao[j] <= xiao[i]) ans++;
  }
  for (i = mid + 1; i <= r; i++) {
    j = i - (da[i] - xiao[i]);
    if (j <= mid && j >= l && da[j] <= da[i] && xiao[j] >= xiao[i]) ans++;
  }
  k = t = mid + 1;
  for (i = mid; i >= l; i--) {
    while (t <= r && xiao[t] >= xiao[i]) tong[da[t] - t + mid]++, t++;
    while (k < t && da[k] <= da[i]) tong[da[k] - k + mid]--, k++;
    ans += tong[xiao[i] - i + mid];
  }
  for (i = k; i <= t - 1; i++) tong[da[i] - i + mid]--;
  k = t = mid;
  for (i = mid + 1; i <= r; i++) {
    while (k >= l && xiao[k] >= xiao[i]) tong[da[k] + k - mid]++, k--;
    while (t > k && da[t] <= da[i]) tong[da[t] + t - mid]--, t--;
    ans += tong[xiao[i] + i - mid];
  }
  for (i = k + 1; i <= t; i++) tong[da[i] + i - mid]--;
  solve(l, mid - 1), solve(mid + 1, r);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &k, &l);
    a[k] = l;
  }
  solve(1, n);
  printf("%I64d\n", ans);
}
