#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#pragma GCC target("avx,tune=native")
using namespace std;
const long long inf = 0x3f3f3f3f;
const double eps = 1e-6;
const long long mod = 1000000007;
inline string getstr(string &s, long long l, long long r) {
  string ret = "";
  for (long long i = l; i <= r; i++) ret.push_back(s[i]);
  return ret;
}
long long modpow(long long x, long long y, long long md = mod) {
  if (y == 0) return 1;
  long long ret = modpow(x, y >> 1, md);
  ret = (long long)ret * ret % md;
  if (y & 1) ret = (long long)ret * x % md;
  return ret;
}
long long n, pos[300005];
vector<pair<long long, long long> > opr, opr2;
long long f[1000005], f2[1000005];
void add(long long x, long long v) {
  opr.push_back(make_pair(x, -v));
  x += 300002;
  f[x] += v;
}
void add2(long long x, long long v) {
  opr2.push_back(make_pair(x, -v));
  x += 300002;
  f2[x] += v;
}
long long calc_max(const vector<pair<long long, long long> > &L,
                   const vector<pair<long long, long long> > &R) {
  long long nl = L.size(), nr = R.size();
  nl--;
  nr--;
  opr.clear();
  opr2.clear();
  long long r = 1;
  multiset<pair<long long, long long> > S;
  S.clear();
  long long ret = 0;
  for (long long i = 1; i <= nl; i++) {
    while (r <= nr && R[r].first < L[i].first) {
      add(R[r].second + r - 1, 1);
      S.insert(make_pair(-R[r].second, R[r].second + r - 1));
      r++;
    }
    while (!S.empty() && -S.begin()->first > L[i].second) {
      long long t = S.begin()->second;
      add(t, -1);
      add2(t + S.begin()->first, 1);
      S.erase(S.begin());
    }
    ret +=
        f[L[i].first - i + 300002] + f2[L[i].first - L[i].second - i + 300002];
  }
  for (long long i = 0; i < opr.size(); i++) {
    f[opr[i].first + 300002] += opr[i].second;
  }
  for (long long i = 0; i < opr2.size(); i++) {
    f2[opr2[i].first + 300002] += opr2[i].second;
  }
  return ret;
}
long long solve(long long l, long long r) {
  if (l == r) return 1;
  long long ret = solve(l, (l + r) / 2) + solve((l + r) / 2 + 1, r);
  vector<pair<long long, long long> > Left(1, make_pair(-inf, inf)),
      Right(1, make_pair(-inf, inf));
  long long Max = -inf, Min = inf;
  for (long long i = (l + r) / 2; i >= l; i--) {
    Max = max(Max, pos[i]);
    Min = min(Min, pos[i]);
    Left.push_back(make_pair(Max, Min));
  }
  Max = -inf;
  Min = inf;
  for (long long i = (l + r) / 2 + 1; i <= r; i++) {
    Max = max(Max, pos[i]);
    Min = min(Min, pos[i]);
    Right.push_back(make_pair(Max, Min));
  }
  long long add = calc_max(Left, Right) + calc_max(Right, Left);
  return ret + add;
}
signed main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    long long r, c;
    scanf("%I64d%I64d", &r, &c);
    pos[r] = c;
  }
  cout << solve(1, n) << endl;
  return 0;
}
