#include <bits/stdc++.h>
using namespace std;
const long long maxn = 6e5 + 10;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
long long n, a[maxn], b[maxn], c[maxn];
vector<long long> ve[maxn], ve2[maxn];
long long divide(long long l, long long r) {
  if (l == r) return 1;
  long long tm = (l + r) >> 1;
  long long ans = divide(l, tm);
  ans += divide(tm + 1, r);
  vector<long long> rmx, rmn, lmx, lmn;
  long long mx = 0, mn = n;
  for (long long i = tm + 1; i <= r; i++) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
    rmx.push_back(mx);
    rmn.push_back(mn);
  }
  reverse(rmn.begin(), rmn.end());
  mx = 0, mn = n;
  set<long long> se, se2;
  for (long long i = tm; i >= l; i--) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
    b[i] = mn - i;
    ve[b[i] + n].push_back(i);
    se.insert(b[i] + n);
    c[i] = mx + i;
    ve2[c[i]].push_back(i);
    se2.insert(c[i]);
    lmx.push_back(mx);
    lmn.push_back(mn);
  }
  reverse(lmn.begin(), lmn.end());
  for (auto x : se) reverse(ve[x].begin(), ve[x].end());
  for (auto x : se2) reverse(ve2[x].begin(), ve2[x].end());
  mx = 0, mn = n;
  for (long long i = tm + 1; i <= r; i++) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
    long long X = lower_bound(lmn.begin(), lmn.end(), mn) - lmn.begin();
    long long Y = upper_bound(lmx.begin(), lmx.end(), mx) - lmx.begin() - 1;
    long long X1 = lower_bound(lmx.begin(), lmx.end(), mx) - lmx.begin();
    long long Y1 = upper_bound(lmn.begin(), lmn.end(), mn) - lmn.begin() - 1;
    X1 = X1 + l;
    X1 = (tm + l) - X1;
    Y1 = (Y1 + l);
    X = (X + l);
    Y = Y + l;
    Y = (tm + l) - Y;
    long long Z = max(X, Y);
    long long L = i - (mx - mn);
    if (Z <= L && L <= tm) ans++;
    long long Q = mx - i + n;
    long long L1 = lower_bound(ve[Q].begin(), ve[Q].end(), Y) - ve[Q].begin();
    long long R1 =
        upper_bound(ve[Q].begin(), ve[Q].end(), Y1) - ve[Q].begin() - 1;
    if (L1 <= R1) {
      ans += R1 - L1 + 1;
    }
    Q = mn + i;
    L1 = lower_bound(ve2[Q].begin(), ve2[Q].end(), X) - ve2[Q].begin();
    R1 = upper_bound(ve2[Q].begin(), ve2[Q].end(), X1) - ve2[Q].begin() - 1;
    if (L1 <= R1) ans += R1 - L1 + 1;
  }
  mx = 0, mn = n;
  for (long long i = tm; i >= l; i--) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);
    long long X = lower_bound(rmn.begin(), rmn.end(), mn) - rmn.begin();
    long long Y = upper_bound(rmx.begin(), rmx.end(), mx) - rmx.begin() - 1;
    X = (tm + 1 + r) - (X + tm + 1);
    Y = Y + tm + 1;
    long long Z = min(X, Y);
    long long R = mx - mn + i;
    if (R <= Z && R >= tm + 1) ans++;
  }
  for (auto x : se) ve[x].clear();
  for (auto x : se2) ve2[x].clear();
  return ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 1, r, c; i <= n; i++) {
    cin >> r >> c;
    a[r] = c;
  }
  cout << divide(1, n);
}
