#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n, a[maxn], lazy[maxn << 2], s[maxn], b[maxn];
vector<int> v1, v2;
pair<int, int> seg[maxn << 2];
long long ans;
void build(int l, int r, int id) {
  seg[id] = {maxn, r - l};
  if (r - l == 1) return;
  int mid = (l + r) >> 1, left = id << 1, right = left | 1;
  build(l, mid, left);
  build(mid, r, right);
  return;
}
void update(int l, int r, int lo, int hi, int val, int id) {
  if (r <= lo || hi <= l) return;
  if (l >= lo && r <= hi) {
    lazy[id] += val;
    seg[id].first += val;
    return;
  }
  int mid = (l + r) >> 1, left = id << 1, right = left | 1;
  update(l, mid, lo, hi, val, left);
  update(mid, r, lo, hi, val, right);
  if (seg[left].first == seg[right].first)
    seg[id] = {seg[left].first, seg[left].second + seg[right].second};
  else
    seg[id] = min(seg[left], seg[right]);
  seg[id].first += lazy[id];
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0, l, r; i < n; ++i) {
    cin >> l >> r;
    a[l - 1] = r - 1;
  }
  v1.push_back(-1);
  v2.push_back(-1);
  for (int i = 0; i < n; ++i) {
    while (v1.back() != -1 && a[v1.back()] < a[i]) v1.pop_back();
    while (v2.back() != -1 && a[v2.back()] > a[i]) v2.pop_back();
    b[i] = v1.back();
    s[i] = v2.back();
    v1.push_back(i);
    v2.push_back(i);
  }
  build(0, n, 1);
  for (int i = 0; i < n; ++i) {
    update(0, n, i, i + 1, -maxn, 1);
    int l = i - 1, r = i;
    while (l != s[i]) {
      l = s[l];
      update(0, n, l + 1, r, a[r - 1] - a[i], 1);
      r = l + 1;
    }
    l = i - 1, r = i;
    while (l != b[i]) {
      l = b[l];
      update(0, n, l + 1, r, a[i] - a[r - 1], 1);
      r = l + 1;
    }
    ans += seg[1].second * (!seg[1].first);
    update(0, n, 0, i + 1, -1, 1);
  }
  cout << ans << '\n';
  return 0;
}
