#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 79, maxn2 = (1 << 19), inf = 1e9 + 79;
class node {
 public:
  int mini, cntmini, lazy, l, r;
};
vector<node> st(maxn2 * 2);
void unlazy(int vr) {
  st[vr * 2 + 1].mini += st[vr].lazy, st[vr * 2 + 2].mini += st[vr].lazy;
  st[vr * 2 + 1].lazy += st[vr].lazy, st[vr * 2 + 2].lazy += st[vr].lazy;
  st[vr].lazy = 0;
}
void merge(int vr) {
  st[vr].mini = min(st[vr * 2 + 1].mini, st[vr * 2 + 2].mini);
  st[vr].cntmini = 0;
  if (st[vr].mini == st[vr * 2 + 1].mini)
    st[vr].cntmini += st[vr * 2 + 1].cntmini;
  if (st[vr].mini == st[vr * 2 + 2].mini)
    st[vr].cntmini += st[vr * 2 + 2].cntmini;
}
void add(int li, int ri, int delta, int vr = 0) {
  if (ri < st[vr].l || li > st[vr].r) return;
  if (li <= st[vr].l && st[vr].r <= ri) {
    st[vr].mini += delta;
    st[vr].lazy += delta;
    return;
  }
  unlazy(vr);
  add(li, ri, delta, vr * 2 + 1), add(li, ri, delta, vr * 2 + 2);
  merge(vr);
}
pair<int, int> count_mini(int li, int ri, int vr = 0) {
  if (ri < st[vr].l || li > st[vr].r) return {inf, 0};
  if (li <= st[vr].l && st[vr].r <= ri) return {st[vr].mini, st[vr].cntmini};
  unlazy(vr);
  pair<int, int> p1 = count_mini(li, ri, vr * 2 + 1),
                 p2 = count_mini(li, ri, vr * 2 + 2);
  if (p1.first < p2.first) return p1;
  if (p2.first < p1.first) return p2;
  return {p1.first, p1.second + p2.second};
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0, r, c; i < n; i++) {
    cin >> r >> c;
    p[--r] = c;
  }
  for (int i = maxn2 - 1; i < maxn2 * 2; i++)
    st[i].l = st[i].r = i - (maxn2 - 1), st[i].mini = inf, st[i].cntmini = 0;
  for (int i = 0; i < n; i++)
    st[i + maxn2 - 1].mini = i, st[i + maxn2 - 1].cntmini = 1;
  for (int i = maxn2 - 2; i >= 0; i--)
    st[i].l = st[i * 2 + 1].l, st[i].r = st[i * 2 + 2].r, merge(i);
  vector<pair<int, int> > mini{{-1, -1}}, maxi{{inf, -1}};
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    while (mini.back().first > p[i]) {
      add(mini[mini.size() - 2].second + 1, mini.back().second,
          mini.back().first - p[i]);
      mini.pop_back();
    }
    mini.push_back({p[i], i});
    while (maxi.back().first < p[i]) {
      add(maxi[maxi.size() - 2].second + 1, maxi.back().second,
          p[i] - maxi.back().first);
      maxi.pop_back();
    }
    maxi.push_back({p[i], i});
    ans += count_mini(0, i).second;
  }
  cout << ans << "\n";
  return 0;
}
