#include <bits/stdc++.h>
using namespace std;
int n, num[300005], Lmx[300005], Lmi[300005], Rmx[300005], Rmi[300005],
    cnt[300005 * 2];
void Rd(int &res) {
  res = 0;
  char c;
  while (c = getchar(), c < 48)
    ;
  do res = res * 10 + (c & 15);
  while (c = getchar(), c > 47);
}
long long solve(int L, int R) {
  if (L == R) return 1;
  int mid = L + R >> 1, a, b;
  long long res = solve(L, mid) + solve(mid + 1, R);
  Lmi[mid] = Lmx[mid] = num[mid];
  for (int i = mid - 1; i >= L; i--) {
    Lmi[i] = min(Lmi[i + 1], num[i]);
    Lmx[i] = max(Lmx[i + 1], num[i]);
  }
  Rmi[mid + 1] = Rmx[mid + 1] = num[mid + 1];
  for (int i = mid + 2; i <= R; i++) {
    Rmi[i] = min(Rmi[i - 1], num[i]);
    Rmx[i] = max(Rmx[i - 1], num[i]);
  }
  for (int i = mid; i >= L; i--) {
    int mi = Lmi[i], mx = Lmx[i];
    int r = mx - mi + i;
    if (r > R || r <= mid || Rmi[r] < mi || Rmx[r] > mx) continue;
    res++;
  }
  for (int i = mid + 1; i <= R; i++) {
    int mi = Rmi[i], mx = Rmx[i];
    int l = i - mx + mi;
    if (l < L || l >= mid + 1 || Lmi[l] < mi || Lmx[l] > mx) continue;
    res++;
  }
  a = mid + 1, b = mid + 1;
  for (int i = mid; i >= L; i--) {
    int lmi = Lmi[i], lmx = Lmx[i];
    int val = lmx + i;
    while (a <= R && Rmx[a] < lmx) cnt[a + Rmi[a]]++, a++;
    while (b <= R && Rmi[b] > lmi) cnt[b + Rmi[b]]--, b++;
    if (a < b) continue;
    res += cnt[val];
  }
  for (int i = mid + 1; i <= R; i++) cnt[i + Rmi[i]] = 0;
  a = mid, b = mid;
  for (int i = mid + 1; i <= R; i++) {
    int rmi = Rmi[i], rmx = Rmx[i];
    int val = Rmx[i] - i + 300005;
    while (a >= L && Lmx[a] < rmx) cnt[Lmi[a] - a + 300005]++, a--;
    while (b >= L && Lmi[b] > rmi) cnt[Lmi[b] - b + 300005]--, b--;
    if (b < a) continue;
    res += cnt[val];
  }
  for (int i = mid; i >= L; i--) cnt[Lmi[i] - i + 300005] = 0;
  return res;
}
int main() {
  Rd(n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    Rd(x), Rd(y);
    num[y] = x;
  }
  cout << solve(1, n) << endl;
  return 0;
}
