#include <bits/stdc++.h>
using namespace std;
const int MAX = 300005;
int a[MAX];
int l[MAX];
int l0[MAX];
struct SegTree {
  int lz[4 * MAX];
  pair<int, int> t[4 * MAX];
  pair<int, int> combine(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) {
      return {a.first, a.second + b.second};
    }
    if (a.first < b.first) {
      return a;
    }
    return b;
  }
  void push(int n) {
    if (lz[n] == 0) {
      return;
    }
    t[n].first += lz[n];
    if (2 * n < 4 * MAX) {
      lz[2 * n] += lz[n];
      lz[2 * n + 1] += lz[n];
    }
    lz[n] = 0;
  }
  void build(int n, int a, int b) {
    if (a == b) {
      t[n].second = 1;
      return;
    }
    build(2 * n, a, (a + b) / 2);
    build(2 * n + 1, (a + b) / 2 + 1, b);
    t[n] = combine(t[2 * n], t[2 * n + 1]);
  }
  void upd(int n, int a, int b, int i, int j, int v) {
    if (i <= a && b <= j) {
      lz[n] += v;
      push(n);
      return;
    }
    push(n);
    if (i > b || j < a) {
      return;
    }
    upd(2 * n, a, (a + b) / 2, i, j, v);
    upd(2 * n + 1, (a + b) / 2 + 1, b, i, j, v);
    t[n] = combine(t[2 * n], t[2 * n + 1]);
  }
  pair<int, int> qry(int n, int a, int b, int i, int j) {
    push(n);
    if (i <= a && b <= j) {
      return t[n];
    }
    if (i > b || j < a) {
      return {MAX, 0};
    }
    return combine(qry(2 * n, a, (a + b) / 2, i, j),
                   qry(2 * n + 1, (a + b) / 2 + 1, b, i, j));
  }
};
SegTree seg, minis, maxis;
int main() {
  vector<pair<int, int> > vec;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    vec.push_back({x, y});
  }
  sort(vec.begin(), vec.end());
  for (int i = 1; i <= n; i++) {
    a[i] = vec[i - 1].second;
  }
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    while (!v.empty() && a[i] > a[v.back()]) {
      v.pop_back();
    }
    if (!v.empty()) {
      l[i] = v.back();
    }
    v.push_back(i);
  }
  v.clear();
  for (int i = 1; i <= n; i++) {
    while (!v.empty() && -a[i] > -a[v.back()]) {
      v.pop_back();
    }
    if (!v.empty()) {
      l0[i] = v.back();
    }
    v.push_back(i);
  }
  seg.build(1, 1, n);
  minis.build(1, 1, n);
  maxis.build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    seg.upd(1, 1, n, i, i, i - n);
  }
  long long answer = 0;
  for (int i = n; i >= 1; i--) {
    int lb = l[i + 1];
    int st = i;
    while (st > lb) {
      int val = a[st] - maxis.qry(1, 1, n, st, st).first;
      seg.upd(1, 1, n, l[st] + 1, st, val);
      maxis.upd(1, 1, n, l[st] + 1, st, val);
      st = l[st];
    }
    lb = l0[i + 1];
    st = i;
    while (st > lb) {
      int val = a[st] - minis.qry(1, 1, n, st, st).first;
      seg.upd(1, 1, n, l0[st] + 1, st, -val);
      minis.upd(1, 1, n, l0[st] + 1, st, val);
      st = l0[st];
    }
    pair<int, int> ans = seg.qry(1, 1, n, 1, i);
    answer += ans.second;
    seg.upd(1, 1, n, 1, i, 1);
  }
  printf("%lld\n", answer);
}
