#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2000 * 1000 + 2, MOD = 1000 * 1000 * 1000 + 7;
long long a[MAXN], seg[MAXN], lazy[MAXN], num[MAXN], n;
stack<long long> mn, mx;
void upd(long long ind, long long val) {
  lazy[ind] += val;
  seg[ind] += val;
  return;
}
void shift(long long ind) {
  upd((ind << 1), lazy[ind]);
  upd(((ind << 1) | 1), lazy[ind]);
  lazy[ind] = 0;
  return;
}
void merge(long long ind) {
  seg[ind] = min(seg[(ind << 1)], seg[((ind << 1) | 1)]);
  num[ind] = 0;
  if (seg[(ind << 1)] == seg[ind]) num[ind] += num[(ind << 1)];
  if (seg[((ind << 1) | 1)] == seg[ind]) num[ind] += num[((ind << 1) | 1)];
  return;
}
void build(long long b = 0, long long e = n, long long ind = 1) {
  if (b + 1 == e) {
    num[ind] = 1;
    return;
  }
  build(b, ((b + e) / 2), (ind << 1));
  build(((b + e) / 2), e, ((ind << 1) | 1));
  merge(ind);
  return;
}
void add(long long l, long long r, long long val, long long b = 0,
         long long e = n, long long ind = 1) {
  if (l >= e || b >= r) {
    return;
  }
  if (l <= b && e <= r) {
    upd(ind, val);
    return;
  }
  shift(ind);
  add(l, r, val, b, ((b + e) / 2), (ind << 1));
  add(l, r, val, ((b + e) / 2), e, ((ind << 1) | 1));
  merge(ind);
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long r, c;
    cin >> r >> c;
    r--;
    c--;
    a[r] = c;
  }
  build();
  long long ans = 0;
  mn.push(n);
  mx.push(n);
  for (long long l = n - 1; l > -1; l--) {
    long long last = 0;
    while (mx.top() != n && a[mx.top()] < a[l]) {
      add(l, mx.top(), a[mx.top()] - last);
      last = a[mx.top()];
      mx.pop();
    }
    add(l, mx.top(), a[l] - last);
    mx.push(l);
    last = 0;
    while (mn.top() != n && a[mn.top()] > a[l]) {
      add(l, mn.top(), last - a[mn.top()]);
      last = a[mn.top()];
      mn.pop();
    }
    add(l, mn.top(), last - a[l]);
    mn.push(l);
    add(l + 1, n, -1);
    ans += (seg[1] == 0 ? num[1] : 0) - l;
  }
  cout << ans << endl;
  return 0;
}
