#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int n;
int a[maxn], tr[maxn << 2], lzy[maxn << 2], c[maxn << 2];
pair<int, int> mx[maxn], mn[maxn];
unordered_map<int, int> mp;
void up(int rt) {
  tr[rt] = min(tr[rt << 1], tr[rt << 1 | 1]);
  c[rt] = (tr[rt << 1] == tr[rt] ? c[rt << 1] : 0) +
          (tr[rt << 1 | 1] == tr[rt] ? c[rt << 1 | 1] : 0);
}
void build(int rt, int l, int r) {
  c[rt] = r - l + 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid);
  build(rt << 1 | 1, mid + 1, r);
}
void down(int rt) {
  if (lzy[rt]) {
    lzy[rt << 1] += lzy[rt];
    lzy[rt << 1 | 1] += lzy[rt];
    tr[rt << 1] += lzy[rt];
    tr[rt << 1 | 1] += lzy[rt];
    lzy[rt] = 0;
  }
}
void update(int rt, int l, int r, int L, int R, int w) {
  if (L <= l && r <= R) {
    tr[rt] += w;
    lzy[rt] += w;
    return;
  }
  down(rt);
  int mid = (l + r) >> 1;
  if (mid >= L) update(rt << 1, l, mid, L, R, w);
  if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, w);
  up(rt);
}
int main() {
  scanf("%d", &n);
  for (int i = 1, x, y; i <= n; i++) {
    scanf("%d %d", &x, &y);
    a[x] = y;
  }
  build(1, 1, n);
  int top1 = 0, top2 = 0;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    while (top1 && mx[top1].second < a[i]) {
      update(1, 1, n, mx[top1 - 1].first + 1, mx[top1].first, -mx[top1].second);
      top1--;
    }
    while (top2 && mn[top2].second > a[i]) {
      update(1, 1, n, mn[top2 - 1].first + 1, mn[top2].first, mn[top2].second);
      top2--;
    }
    update(1, 1, n, mp[a[i]] + 1, i, -1);
    mx[++top1] = mn[++top2] = make_pair(i, a[i]);
    mp[a[i]] = i;
    update(1, 1, n, mx[top1 - 1].first + 1, i, a[i]);
    update(1, 1, n, mn[top2 - 1].first + 1, i, -a[i]);
    ans += c[1];
  }
  printf("%lld\n", ans);
  return 0;
}
