#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 3;
struct node {
  int xi;
  int yi;
} a[N];
int n, m;
int c[N << 1];
struct th {
  int maxx;
  int minn;
};
th tl[N], tr[N];
int sum[N];
long long ans = 0;
inline bool cmp(node x, node y) { return x.xi < y.xi; }
inline int read() {
  int v = 0, c = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') c = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    v = v * 10 + ch - 48;
    ch = getchar();
  }
  return v * c;
}
inline void solve(int l, int r) {
  if (l == r) {
    ans++;
    return;
  }
  int mid = (l + r) >> 1;
  solve(l, mid);
  solve(mid + 1, r);
  tl[mid].maxx = tl[mid].minn = a[mid].yi;
  for (int i = mid - 1; i >= l; --i) {
    tl[i].maxx = max(tl[i + 1].maxx, a[i].yi);
    tl[i].minn = min(tl[i + 1].minn, a[i].yi);
  }
  tr[mid + 1].maxx = tr[mid + 1].minn = a[mid + 1].yi;
  for (int i = mid + 2; i <= r; ++i) {
    tr[i].maxx = max(tr[i - 1].maxx, a[i].yi);
    tr[i].minn = min(tr[i - 1].minn, a[i].yi);
  }
  for (int i = mid; i >= l; --i) {
    int to = i + tl[i].maxx - tl[i].minn;
    if (to <= mid || to > r) continue;
    if (tr[to].maxx < tl[i].maxx && tr[to].minn > tl[i].minn) ans++;
  }
  for (int i = mid + 1; i <= r; ++i) {
    int to = i - (tr[i].maxx - tr[i].minn);
    if (to >= mid + 1 || to < l) continue;
    if (tl[to].maxx < tr[i].maxx && tr[i].minn < tl[to].minn) ans++;
  }
  int ll = mid + 1, rr = mid + 1;
  for (int i = mid; i >= l; --i) {
    while (rr <= r && tr[rr].maxx < tl[i].maxx) c[rr + tr[rr].minn]++, ++rr;
    while (ll < rr && tr[ll].minn > tl[i].minn) c[ll + tr[ll].minn]--, ++ll;
    ans += c[i + tl[i].maxx];
  }
  for (int i = ll; i < rr; ++i) c[i + tr[i].minn]--;
  ll = rr = mid;
  for (int i = mid + 1; i <= r; ++i) {
    while (ll >= l && tl[ll].maxx < tr[i].maxx) c[ll - tl[ll].minn + N]++, --ll;
    while (rr > ll && tl[rr].minn > tr[i].minn) c[rr - tl[rr].minn + N]--, --rr;
    ans += c[i - tr[i].maxx + N];
  }
  for (int i = rr; i > ll; --i) c[i - tl[i].minn + N]--;
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i].xi = read(), a[i].yi = read(), sum[a[i].xi] = a[i].yi;
  for (int i = 1; i <= n; ++i) a[i].yi = sum[i];
  solve(1, n);
  cout << ans << endl;
  return 0;
}
