#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 + 10;
struct SegTree {
  int l, r, m;
  SegTree *cl, *cr;
  int v, c, lazy;
  SegTree(int l, int r)
      : l(l),
        r(r),
        m((l + r) / 2),
        cl(nullptr),
        cr(nullptr),
        v(0),
        c(r - l + 1),
        lazy(0) {
    if (l != r) {
      cl = new SegTree(l, m);
      cr = new SegTree(m + 1, r);
    }
  }
  void pushdown() {
    if (!lazy) return;
    if (l != r) {
      cl->lazy += lazy;
      cr->lazy += lazy;
    }
    v += lazy;
    lazy = 0;
  }
  void update() {
    cl->pushdown();
    cr->pushdown();
    if (cl->v < cr->v) {
      v = cl->v;
      c = cl->c;
    } else if (cr->v < cl->v) {
      v = cr->v;
      c = cr->c;
    } else {
      v = cl->v;
      c = cl->c + cr->c;
    }
  }
  void add(int ql, int qr, int qv) {
    if (qr < l || ql > r) return;
    if (ql <= l && qr >= r)
      lazy += qv;
    else {
      pushdown();
      cl->add(ql, qr, qv);
      cr->add(ql, qr, qv);
      update();
    }
  }
  pair<int, int> query(int ql, int qr) {
    if (ql > r || qr < l) return make_pair(INT_MAX, 0);
    pushdown();
    if (ql <= l && qr >= r) return make_pair(v, c);
    pair<int, int> res_l = cl->query(ql, qr), res_r = cr->query(ql, qr);
    if (res_l.first < res_r.first) return res_l;
    if (res_r.first < res_l.first) return res_r;
    return make_pair(res_l.first, res_l.second + res_r.second);
  }
} * segt;
int N, A[MAX_N];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    A[a] = b;
  }
  segt = new SegTree(1, N);
  stack<pair<int, int> > stk_min, stk_max;
  stk_min.emplace(INT_MIN, 0);
  stk_max.emplace(INT_MAX, 0);
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    while (stk_min.top().first >= A[i]) {
      pair<int, int> t = stk_min.top();
      stk_min.pop();
      segt->add(stk_min.top().second + 1, t.second, t.first);
    }
    segt->add(stk_min.top().second + 1, i, -A[i]);
    stk_min.emplace(A[i], i);
    while (stk_max.top().first <= A[i]) {
      pair<int, int> t = stk_max.top();
      stk_max.pop();
      segt->add(stk_max.top().second + 1, t.second, -t.first);
    }
    segt->add(stk_max.top().second + 1, i, A[i]);
    stk_max.emplace(A[i], i);
    segt->add(1, i - 1, -1);
    pair<int, int> res = segt->query(1, i);
    ans += res.second;
  }
  printf("%lld\n", ans);
}
