#include <bits/stdc++.h>
using namespace std;
long long n, a[500010], x, r, b1[1000010], b2[1000010], p1[1000010],
    p2[1000010], p3[1000010], p4[1000010], ans1, ans2;
void solve(long long l, long long r) {
  if (l == r) return;
  long long mid = (l + r) / 2;
  solve(l, mid);
  solve(mid + 1, r);
  p3[mid] = 1e9;
  p4[mid] = 0;
  p1[mid + 1] = 1e9;
  p2[mid + 1] = 0;
  long long t1 = mid, t2 = mid;
  for (long long i = mid + 1; i <= r; i++) {
    p3[i] = min(p3[i - 1], a[i]);
    p4[i] = max(p4[i - 1], a[i]);
  }
  for (long long i = mid; i >= l; i--) {
    p1[i] = min(p1[i + 1], a[i]);
    p2[i] = max(p2[i + 1], a[i]);
  }
  for (long long i = mid + 1; i <= r; i++) {
    while (t1 >= l && p1[t1] > p3[i] && p2[t1] < p4[i]) {
      b1[p1[t1] - t1 + n]++;
      t1--;
    }
    while (t2 >= l && p2[t2] < p4[i]) {
      b2[p1[t2] - t2 + n]++;
      t2--;
    }
    if (t1 != mid && t1 + 1 <= i - p4[i] + p3[i]) ans2++;
    ans2 += b2[p4[i] - i + n] - b1[p4[i] - i + n];
  }
  for (long long i = l; i <= mid; i++)
    b2[p1[i] - i + n] = b1[p1[i] - i + n] = 0;
  t1 = t2 = mid + 1;
  for (long long i = mid; i >= l; i--) {
    while (t1 <= r && p3[t1] > p1[i] && p4[t1] < p2[i]) {
      b1[t1 + p3[t1]]++;
      t1++;
    }
    while (t2 <= r && p4[t2] < p2[i]) {
      b2[t2 + p3[t2]]++;
      t2++;
    }
    if (t1 != mid + 1 && t1 - 1 - i >= p2[i] - p1[i]) ans1++;
    ans1 += b2[i + p2[i]] - b1[i + p2[i]];
  }
  for (long long i = mid + 1; i <= r; i++) b2[i + p3[i]] = b1[i + p3[i]] = 0;
}
int main() {
  scanf("%I64d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &x);
    scanf("%I64d", &a[x]);
  }
  solve(1, n);
  printf("%I64d", ans1 + ans2 + n);
}
