#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
const int N = 1e6 + 100, OO = 1e9 + 7, M = 1e9 + 7, P = 499122177, T = 2e3,
          SQ = 280, lg = 64, lg2 = 32;
int a[N], mn[N], mx[N], s[N];
long long ans = 0;
void get(int b, int mid, int e) {
  mn[mid] = OO, mx[mid] = 0;
  for (int i = mid - 1; i >= b; i--) {
    mn[i] = min(mn[i + 1], a[i]);
    mx[i] = max(mx[i + 1], a[i]);
  }
  int l = OO, r = 0, tp = mid, tt = mid;
  for (int i = mid; i < e; i++) {
    l = min(l, a[i]), r = max(r, a[i]);
    while (tp > b) {
      if (mx[tp - 1] < r)
        tp--, s[mn[tp] + mid - tp - 1]++;
      else
        break;
    }
    while (tp < tt) {
      if (mn[tt - 1] > l)
        tt--, s[mn[tt] + mid - tt - 1]--;
      else
        break;
    }
    if (tp == mid) continue;
    int cnt = mid - (r - l + 1 - (i - mid + 1));
    if (cnt >= b && mn[cnt] > l && mx[cnt] < r && cnt < mid) ans++;
    if (mn[tp] < l) ans += s[r - (i - mid + 1)];
  }
  for (int i = b; i < mid; i++) s[mn[i] + mid - i - 1] = 0;
}
void cal(int b, int e) {
  if (e - b == 1) {
    ans++;
    return;
  }
  int mid = (b + e) >> 1, mid1;
  mid1 = (e - mid) + b;
  cal(b, mid), cal(mid, e);
  get(b, mid, e);
  reverse(a + b, a + e);
  get(b, mid1, e);
  reverse(a + b, a + e);
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[x] = y;
  }
  cal(1, n + 1);
  cout << ans << endl;
  return 0;
}
