#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 * 1000 + 10, maxk = 20;
int n, a[maxn], x, y, zero, posl, posr, sum[4 * maxn], rmqh[maxn][maxk],
    rmql[maxn][maxk], t = -1, ted[maxn], mn, mx, mnn, mxx;
bool data[4 * maxn];
long long ans;
inline void push(int x, int &l, int &r) {
  if (data[x]) {
    data[2 * x + 1] = data[2 * x + 2] = 1;
    sum[2 * x + 1] = sum[2 * x + 2] = 0;
    data[x] = 0;
  }
}
int get(int x, int &l, int &r, int &ql, int &qr) {
  if (l >= qr || ql >= r) return 0;
  if (ql <= l && r <= qr) return sum[x];
  int mid = (l + r) / 2;
  push(x, l, r);
  return get(2 * x + 1, l, mid, ql, qr) + get(2 * x + 2, mid, r, ql, qr);
}
void cng(int x, int &l, int &r, int &m) {
  if (r - l == 1) {
    sum[x] = 1;
    return;
  }
  int mid = (l + r) / 2;
  push(x, l, r);
  if (m < mid)
    cng(2 * x + 1, l, mid, m);
  else
    cng(2 * x + 2, mid, r, m);
  sum[x] = sum[2 * x + 1] + sum[2 * x + 2];
}
void upd(int x, int &l, int &r, int ql, int &qr) {
  if (l >= qr || ql >= r) return;
  if (ql <= l && r <= qr) {
    data[x] = 1;
    sum[x] = 0;
    return;
  }
  int mid = (l + r) / 2;
  push(x, l, r);
  upd(2 * x + 1, l, mid, ql, qr);
  upd(2 * x + 2, mid, r, ql, qr);
  sum[x] = sum[2 * x + 1] + sum[2 * x + 2];
}
void solve(int &l, int &r) {
  if (r - l == 1) {
    ans++;
    return;
  }
  int mid = (l + r) / 2;
  solve(l, mid);
  solve(mid, r);
  posl = mid - 1;
  posr = mid + 1;
  mnn = mn = min(a[mid - 1], a[mid]);
  mxx = mx = max(a[mid - 1], a[mid]);
  while (posr <= r) {
    if (posr < r &&
        max(rmqh[mn][ted[mx - mn + 1]],
            rmqh[mx - (1 << ted[mx - mn + 1]) + 1][ted[mx - mn + 1]]) >= posr) {
      mn = min(mn, a[posr]);
      mx = max(mx, a[posr]);
      mnn = min(mnn, a[posr]);
      mxx = max(mxx, a[posr]);
      posr++;
    } else if (max(rmqh[mn][ted[mx - mn + 1]],
                   rmqh[mx - (1 << ted[mx - mn + 1]) + 1][ted[mx - mn + 1]]) <
               posr) {
      upd(zero, zero, n,
          min(rmql[mnn][ted[mxx - mnn + 1]],
              rmql[mxx - (1 << ted[mxx - mnn + 1]) + 1][ted[mxx - mnn + 1]]) +
              1,
          mid);
      while (posl >= l) {
        if (max(rmqh[min(mn, a[posl])]
                    [ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]],
                rmqh[max(mx, a[posl]) -
                     (1 << ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]) + 1]
                    [ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]]) >= posr ||
            min(rmql[min(mn, a[posl])]
                    [ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]],
                rmql[max(mx, a[posl]) -
                     (1 << ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]) + 1]
                    [ted[max(mx, a[posl]) - min(mn, a[posl]) + 1]]) < l)
          break;
        mn = min(mn, a[posl]);
        mx = max(mx, a[posl]);
        if (mx - mn + 1 == posr - posl) cng(zero, zero, n, posl);
        posl--;
      }
      ans += get(zero, zero, n, l, mid);
      if (posr < r) {
        mn = min(mn, a[posr]);
        mx = max(mx, a[posr]);
        mnn = min(mnn, a[posr]);
        mxx = max(mxx, a[posr]);
      }
      posr++;
    } else if (posr == r)
      break;
  }
  upd(zero, zero, n, l, mid);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    x--;
    y--;
    a[x] = y;
    rmqh[y][0] = rmql[y][0] = x;
  }
  for (int i = 1; i < maxn; i++) {
    if (i == (1 << (t + 1))) t++;
    ted[i] = t;
  }
  for (int i = 1; i < maxk; i++) {
    for (int j = 0; j + (1 << i) <= n; j++) {
      rmqh[j][i] = max(rmqh[j][i - 1], rmqh[j + (1 << (i - 1))][i - 1]);
      rmql[j][i] = min(rmql[j][i - 1], rmql[j + (1 << (i - 1))][i - 1]);
    }
  }
  solve(zero, n);
  printf("%I64d", ans);
  return 0;
}
