#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
void fin() {}
const bool flag = 0;
const int N = 3e5 + 10;
int a[N], mx[N], mn[N], cnt[N + N + 10];
ll calc(int l, int m, int r) {
  mx[m] = mn[m] = a[m];
  for (int i = m - 1; i >= l; --i) {
    mn[i] = min(mn[i + 1], a[i]);
    mx[i] = max(mx[i + 1], a[i]);
  }
  mx[m + 1] = mn[m + 1] = a[m + 1];
  for (int i = m + 2; i <= r; ++i) {
    mn[i] = min(mn[i - 1], a[i]);
    mx[i] = max(mx[i - 1], a[i]);
  }
  ll res = 0;
  for (int i = m; i >= l; --i) {
    int len = mx[i] - mn[i];
    if (m < i + len && i + len <= r && mx[i + len] < mx[i] &&
        mn[i + len] > mn[i]) {
      res++;
    }
  }
  int cur_mx = m + 1, cur_mn = m + 1;
  for (int i = m; i >= l; --i) {
    while (cur_mx <= r && mx[cur_mx] < mx[i]) {
      cnt[cur_mx + mn[cur_mx]]++, cur_mx++;
    }
    while (cur_mn < cur_mx && mn[cur_mn] > mn[i]) {
      cnt[cur_mn + mn[cur_mn]]--, cur_mn++;
    }
    res += cnt[i + mx[i]];
  }
  for (int i = m + 1; i <= r; ++i) {
    cnt[i + mn[i]] = 0;
  }
  return res;
}
ll dnc(int l, int r) {
  if (l == r) return 1;
  int m = l + r >> 1;
  ll res = calc(l, m, r);
  reverse(a + l, a + r + 1);
  res += calc(l, m - (r - l + 1) % 2, r);
  reverse(a + l, a + r + 1);
  return res + dnc(l, m) + dnc(m + 1, r);
}
void ma1n() {
  int n;
  cin >> n;
  vector<pii> b(n);
  for (pii& first : b) {
    cin >> first.first >> first.second;
  }
  sort((b).begin(), (b).end());
  for (int i = 0; i < n; ++i) {
    a[i] = b[i].second;
  }
  cout << dnc(0, n - 1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr), fin();
  int ts = 1;
  if (flag) {
    cin >> ts;
  }
  while (ts--) {
    ma1n();
  }
  return 0;
}
