#include <bits/stdc++.h>
using namespace std;
int n;
int top1, top2;
int q1[300010], q2[300010], pos1[300010], pos2[300010];
long long ans;
int a[300010];
int tag[1200010];
pair<int, int> tree[1200010];
inline int read() {
  int ans = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch >= '0' && ch <= '9')
    ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  return ans;
}
pair<int, int> merge(pair<int, int> x, pair<int, int> y) {
  if (x.first == y.first) return make_pair(x.first, x.second + y.second);
  if (x.first < y.first) return x;
  return y;
}
void build(int rex, int l, int r) {
  if (l == r) {
    tree[rex] = make_pair(0, 1);
    return;
  }
  int mid = (l + r) >> 1;
  build(rex + rex, l, mid), build(rex + rex + 1, mid + 1, r);
  tree[rex] = merge(tree[rex + rex], tree[rex + rex + 1]);
}
void pushdown(int rex) {
  if (tag[rex]) {
    tag[rex + rex] += tag[rex], tag[rex + rex + 1] += tag[rex];
    tree[rex + rex].first += tag[rex], tree[rex + rex + 1].first += tag[rex];
    tag[rex] = 0;
  }
}
void ins(int rex, int l, int r, int x, int y, int k) {
  if (x > y) return;
  if (l == x && r == y) {
    tag[rex] += k;
    tree[rex].first += k;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(rex);
  if (y <= mid)
    ins(rex + rex, l, mid, x, y, k);
  else if (x > mid)
    ins(rex + rex + 1, mid + 1, r, x, y, k);
  else
    ins(rex + rex, l, mid, x, mid, k),
        ins(rex + rex + 1, mid + 1, r, mid + 1, y, k);
  tree[rex] = merge(tree[rex + rex], tree[rex + rex + 1]);
}
pair<int, int> query(int rex, int l, int r, int x, int y) {
  if (l == x && r == y) return tree[rex];
  int mid = (l + r) >> 1;
  pushdown(rex);
  if (y <= mid)
    return query(rex + rex, l, mid, x, y);
  else if (x > mid)
    return query(rex + rex + 1, mid + 1, r, x, y);
  else
    return merge(query(rex + rex, l, mid, x, mid),
                 query(rex + rex + 1, mid + 1, r, mid + 1, y));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    a[x] = y;
  }
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    while (top1 && q1[top1] <= a[i]) {
      ins(1, 1, n, pos1[top1 - 1] + 1, pos1[top1], -q1[top1]);
      top1--;
    }
    pos1[++top1] = i;
    q1[top1] = a[i];
    ins(1, 1, n, pos1[top1 - 1] + 1, pos1[top1], q1[top1]);
    while (top2 && q2[top2] >= a[i]) {
      ins(1, 1, n, pos2[top2 - 1] + 1, pos2[top2], q2[top2]);
      top2--;
    }
    pos2[++top2] = i;
    q2[top2] = a[i];
    ins(1, 1, n, pos2[top2 - 1] + 1, pos2[top2], -q2[top2]);
    ins(1, 1, n, 1, i - 1, -1);
    ans += query(1, 1, n, 1, i).second;
  }
  printf("%lld\n", ans);
}
