#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 5, inf = 1e18, mod = 1e9 + 7;
long long n, ans, mx[maxn], mn[maxn], cnt[maxn];
pair<long long, long long> p[maxn];
void fix(long long l, long long r) {
  long long mid = (l + r) / 2ll;
  mx[mid] = p[mid].second;
  mn[mid] = p[mid].second;
  for (long long i = mid + 1; i < r; i++) {
    mx[i] = max(mx[i - 1], p[i].second);
    mn[i] = min(mn[i - 1], p[i].second);
  }
  mx[mid - 1] = p[mid - 1].second;
  mn[mid - 1] = p[mid - 1].second;
  for (long long i = mid - 2; i >= l; i--) {
    mx[i] = max(p[i].second, mx[i + 1]);
    mn[i] = min(p[i].second, mn[i + 1]);
  }
}
void cal(long long l, long long r) {
  long long mid = (l + r) / 2ll;
  for (long long i = mid - 2; i >= l; i--) {
    long long x = mx[i], y = mn[i];
    long long len = x - y + 1;
    if (len < mid - i + 1) continue;
    long long j = i + len - 1;
    if (j >= r) continue;
    if (mx[j] > mx[i] || mn[j] < mn[i]) continue;
    ans++;
  }
  for (long long i = mid + 1; i < r; i++) {
    long long x = mx[i], y = mn[i];
    long long len = x - y + 1;
    if (len < i - mid + 2) continue;
    long long j = i - len + 1;
    if (j < l) continue;
    if (mx[j] > mx[i] || mn[j] < mn[i]) continue;
    ans++;
  }
}
void cal2(long long l, long long r) {
  long long mid = (l + r) / 2ll;
  long long p1 = mid - 1, p2 = p1;
  for (long long i = mid - 1; i >= l; i--) {
    while (p1 < r - 1 && mn[p1 + 1] > mn[i]) {
      p1++;
      cnt[mx[p1] - (p1 - mid + 1) + n]++;
    }
    while (p2 < r - 1 && mx[p2 + 1] < mx[i]) {
      p2++;
      cnt[mx[p2] - (p2 - mid + 1) + n]--;
    }
    if (p2 < p1) ans += cnt[mn[i] + (mid - i) + n - 1];
  }
  for (long long i = mid; i < r; i++) cnt[mx[i] - (i - mid + 1) + n] = 0;
  p1 = mid, p2 = p1;
  for (long long i = mid; i < r; i++) {
    while (p1 > l && mn[p1 - 1] > mn[i]) {
      p1--;
      cnt[mx[p1] - (mid - p1) + n]++;
    }
    while (p2 > l && mx[p2 - 1] < mx[i]) {
      p2--;
      cnt[mx[p2] - (mid - p2) + n]--;
    }
    if (p1 < p2) ans += cnt[mn[i] + (i - mid + 1) + n - 1];
  }
  for (long long i = l; i < mid; i++) cnt[mx[i] - (mid - i) + n] = 0;
}
void solve(long long l, long long r) {
  if (r - l == 1) {
    ans++;
    return;
  }
  long long mid = (l + r) / 2ll;
  solve(l, mid);
  solve(mid, r);
  fix(l, r);
  cal(l, r);
  cal2(l, r);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    p[i] = {x, y};
  }
  sort(p, p + n);
  solve(0, n);
  cout << ans << endl;
}
