#include <bits/stdc++.h>
using namespace std;
int Min[1000010], Max[1000010], a[1000010], sum[2000010];
long long ans = 0;
int n;
void dfs(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  dfs(l, mid);
  dfs(mid + 1, r);
  Min[mid + 1] = Max[mid + 1] = a[mid + 1];
  Min[mid] = Max[mid] = a[mid];
  for (int i = mid - 1; i >= l; i--)
    Min[i] = min(a[i], Min[i + 1]), Max[i] = max(a[i], Max[i + 1]);
  for (int i = mid + 2; i <= r; i++)
    Min[i] = min(a[i], Min[i - 1]), Max[i] = max(a[i], Max[i - 1]);
  for (int i = mid; i >= l; i--) {
    int j = Max[i] - Min[i] + i;
    if (j > mid && j <= r && Max[j] < Max[i] && Min[j] > Min[i]) ans++;
  }
  for (int j = mid + 1; j <= r; j++) {
    int i = j - Max[j] + Min[j];
    if (i >= l && i <= mid && Max[j] > Max[i] && Min[j] < Min[i]) ans++;
  }
  int head = mid + 1, tail = mid + 1;
  for (int i = mid; i >= l; i--) {
    while (tail <= r && Min[tail] > Min[i]) sum[Max[tail] - tail + n]++, tail++;
    while (head < tail && Max[head] < Max[i])
      sum[Max[head] - head + n]--, head++;
    ans += (long long)sum[Min[i] - i + n];
  }
  while (head < tail) sum[Max[head] - head + n]--, head++;
  head = tail = mid;
  for (int i = mid + 1; i <= r; i++) {
    while (head >= l && Min[head] > Min[i]) sum[Max[head] + head]++, head--;
    while (head < tail && Max[tail] < Max[i]) sum[Max[tail] + tail]--, tail--;
    ans += (long long)sum[Min[i] + i];
  }
  while (head < tail) sum[Max[tail] + tail]--, tail--;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x] = y;
  }
  dfs(1, n);
  printf("%lld\n", ans + (long long)n);
  return 0;
}
