#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
const int MAXM = 1e6 + 5;
template <typename T>
void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
struct SegmentTree {
  struct Node {
    int lc, rc;
    int tag, Max;
  } a[MAXN * 2];
  int n, root, size;
  void build(int &root, int l, int r) {
    root = ++size;
    a[root].tag = 0;
    a[root].Max = 0;
    if (l == r) return;
    int mid = (l + r) / 2;
    build(a[root].lc, l, mid);
    build(a[root].rc, mid + 1, r);
  }
  void init(int x) {
    n = x, root = size = 0;
    build(root, 1, n);
  }
  void update(int root) {
    a[root].Max = max(a[a[root].lc].Max, a[a[root].rc].Max);
  }
  void pushdown(int root) {
    if (a[root].tag) {
      a[a[root].lc].tag += a[root].tag;
      a[a[root].lc].Max += a[root].tag;
      a[a[root].rc].tag += a[root].tag;
      a[a[root].rc].Max += a[root].tag;
      a[root].tag = 0;
    }
  }
  void modify(int root, int l, int r, int ql, int qr, int d) {
    if (l == ql && r == qr) {
      a[root].tag += d;
      a[root].Max += d;
      return;
    }
    int mid = (l + r) / 2;
    pushdown(root);
    if (mid >= ql) modify(a[root].lc, l, mid, ql, min(mid, qr), d);
    if (mid + 1 <= qr) modify(a[root].rc, mid + 1, r, max(mid + 1, ql), qr, d);
    update(root);
  }
  void modify(int l, int r, int d) { modify(root, 1, n, l, r, d); }
  int query(int root, int l, int r, int ql, int qr) {
    if (l == ql && r == qr) {
      if (a[root].Max < 1) return 0;
      if (l == r) return l;
      int mid = (l + r) / 2;
      pushdown(root);
      if (a[a[root].rc].Max >= 1)
        return query(a[root].rc, mid + 1, r, mid + 1, r);
      else
        return query(a[root].lc, l, mid, l, mid);
    }
    int mid = (l + r) / 2;
    pushdown(root);
    if (mid >= qr)
      return query(a[root].lc, l, mid, ql, qr);
    else if (mid + 1 <= ql)
      return query(a[root].rc, mid + 1, r, ql, qr);
    else {
      int tmp = query(a[root].rc, mid + 1, r, mid + 1, qr);
      if (tmp != 0)
        return tmp;
      else
        return query(a[root].lc, l, mid, ql, mid);
    }
  }
  int query(int l, int r) {
    if (l > r)
      return 0;
    else
      return query(root, 1, n, l, r);
  }
} ST;
struct Node {
  int pl, pr, vl, vr, type;
  vector<int> sons;
} a[MAXN * 2];
int n, tot, b[MAXN];
long long ans;
int leaf(int x) {
  a[++tot].type = 0;
  a[tot].pl = a[tot].pr = x;
  a[tot].vl = a[tot].vr = b[x];
  return tot;
}
int join(int x, int y) {
  if (a[x].vl < a[y].vl) {
    assert(a[x].vr + 1 == a[y].vl);
    a[++tot].type = 1;
    a[tot].pl = a[x].pl, a[tot].vl = a[x].vl;
    a[tot].pr = a[y].pr, a[tot].vr = a[y].vr;
  } else {
    assert(a[y].vr + 1 == a[x].vl);
    a[++tot].type = -1;
    a[tot].pl = a[x].pl, a[tot].vr = a[x].vr;
    a[tot].pr = a[y].pr, a[tot].vl = a[y].vl;
  }
  a[tot].sons.push_back(x);
  a[tot].sons.push_back(y);
  return tot;
}
void buildTree() {
  ST.init(n);
  for (int i = 1; i <= n; i++) leaf(i);
  static int s[MAXN];
  int top = 0;
  static int QMax[MAXN];
  int TMax = 0;
  static int QMin[MAXN];
  int TMin = 0;
  for (int i = 1; i <= n; i++) {
    ST.modify(1, i, 1);
    while (TMax >= 1 && b[QMax[TMax]] < b[i])
      ST.modify(QMax[TMax - 1] + 1, QMax[TMax], b[QMax[TMax]]), TMax--;
    ST.modify(QMax[TMax] + 1, i, -b[i]), QMax[++TMax] = i;
    while (TMin >= 1 && b[QMin[TMin]] > b[i])
      ST.modify(QMin[TMin - 1] + 1, QMin[TMin], -b[QMin[TMin]]), TMin--;
    ST.modify(QMin[TMin] + 1, i, b[i]), QMin[++TMin] = i;
    bool flg = true;
    s[++top] = i;
    while (flg) {
      flg = false;
      if (top >= 2 && a[s[top - 1]].type == 1 &&
          a[s[top - 1]].vr + 1 == a[s[top]].vl) {
        a[s[top - 1]].vr = a[s[top]].vr;
        a[s[top - 1]].pr = a[s[top]].pr;
        a[s[top - 1]].sons.push_back(s[top]);
        flg = true, top--;
      } else if (top >= 2 && a[s[top - 1]].type == -1 &&
                 a[s[top - 1]].vl - 1 == a[s[top]].vr) {
        a[s[top - 1]].vl = a[s[top]].vl;
        a[s[top - 1]].pr = a[s[top]].pr;
        a[s[top - 1]].sons.push_back(s[top]);
        flg = true, top--;
      } else if (top >= 2 && max(a[s[top]].vr, a[s[top - 1]].vr) -
                                     min(a[s[top]].vl, a[s[top - 1]].vl) ==
                                 max(a[s[top]].pr, a[s[top - 1]].pr) -
                                     min(a[s[top]].pl, a[s[top - 1]].pl)) {
        int y = s[top--], x = s[top--];
        s[++top] = join(x, y);
        flg = true;
      } else {
        int tmp = ST.query(1, a[s[top]].pl - 1);
        if (tmp != 0) {
          a[++tot].type = 2;
          a[tot].pl = tmp, a[tot].pr = i;
          a[tot].vl = b[i];
          a[tot].vr = b[i];
          while (a[s[top]].pl >= tmp) {
            chkmin(a[tot].vl, a[s[top]].vl);
            chkmax(a[tot].vr, a[s[top]].vr);
            a[tot].sons.push_back(s[top--]);
          }
          s[++top] = tot;
          flg = true;
        }
      }
    }
  }
  ans = n;
  for (int i = n + 1; i <= tot; i++)
    if (abs(a[i].type) == 1) {
      int k = a[i].sons.size();
      ans += 1ll * k * (k + 1) / 2 - k;
    } else
      ans += 1;
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    read(x), read(y);
    b[x] = y;
  }
  buildTree();
  cout << ans << endl;
  return 0;
}
