#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 100;
const int INF = 1e9 + 7;
int mn[MAXN * 4], cnt[MAXN * 4], laz[MAXN * 4];
int p[MAXN];
void Make(int v, int l, int r) {
  if (r - l == 1) {
    cnt[v] = 1;
    return;
  }
  int mid = (l + r) / 2;
  Make(2 * v, l, mid);
  Make(2 * v + 1, mid, r);
  cnt[v] = r - l;
  return;
}
void add(int v, int l, int r, int f, int e, int x) {
  if (f <= l && r <= e) {
    laz[v] += x;
    mn[v] += x;
    return;
  }
  int mid = (l + r) / 2;
  if (f < mid) {
    add(2 * v, l, mid, f, e, x);
  }
  if (mid < e) {
    add(2 * v + 1, mid, r, f, e, x);
  }
  mn[v] = min(mn[2 * v], mn[2 * v + 1]);
  if (mn[2 * v] == mn[2 * v + 1]) {
    cnt[v] = cnt[2 * v] + cnt[2 * v + 1];
  } else if (mn[2 * v] > mn[2 * v + 1]) {
    cnt[v] = cnt[2 * v + 1];
  } else {
    cnt[v] = cnt[2 * v];
  }
  mn[v] += laz[v];
  return;
}
pair<int, int> ask(int v, int l, int r, int f, int e) {
  if (f <= l && r <= e) {
    return {mn[v], cnt[v]};
  }
  int mid = (l + r) / 2;
  pair<int, int> res, res2;
  res = {INF, 0};
  res2 = {INF, 0};
  if (f < mid) {
    res = ask(2 * v, l, mid, f, e);
  }
  if (mid < e) {
    res2 = ask(2 * v + 1, mid, r, f, e);
  }
  if (res.first == res2.first) {
    return {res.first + laz[v], res2.second + res.second};
  }
  if (res.first > res2.first) {
    return {res2.first + laz[v], res2.second};
  }
  if (res.first < res2.first) {
    return {res.first + laz[v], res.second};
  }
}
int main() {
  int n;
  cin >> n;
  int l, r;
  for (int i = 0; i < n; i++) {
    cin >> l >> r;
    l--;
    r--;
    p[l] = r;
  }
  Make(1, 0, n);
  stack<int> Mx, Mn;
  Mx.push(-1);
  Mn.push(-1);
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    while (Mx.top() != -1 && p[Mx.top()] <= p[i]) {
      int q = Mx.top();
      Mx.pop();
      add(1, 0, n, Mx.top() + 1, q + 1, -p[q]);
      add(1, 0, n, Mx.top() + 1, q + 1, p[i]);
    }
    Mx.push(i);
    while (Mn.top() != -1 && p[Mn.top()] >= p[i]) {
      int q = Mn.top();
      Mn.pop();
      add(1, 0, n, Mn.top() + 1, q + 1, p[q]);
      add(1, 0, n, Mn.top() + 1, q + 1, -p[i]);
    }
    Mn.push(i);
    add(1, 0, n, i, i + 1, i);
    int k = ans;
    ans += ask(1, 0, n, 0, i + 1).second;
  }
  cout << ans << endl;
}
