#include <bits/stdc++.h>
using namespace std;
struct Reader {
  istream& is;
  template <typename T>
  operator T() const {
    T t;
    is >> t;
    return t;
  }
};
const int N = 3e5;
int n;
int b[N];
struct Info {
  int min;
  int count;
  friend Info operator+(Info const& l, Info const& r) {
    if (l.min < r.min) return l;
    if (r.min < l.min) return r;
    return {l.min, l.count + r.count};
  }
} t[N << 2];
int add[N << 2];
Info const& build(int c, int l, int r) {
  if (l == r) return t[c] = {l, 1};
  int m = (l + r) >> 1;
  return t[c] = build(((c) << 1), l, m) + build(((c) << 1 | 1), m + 1, r);
}
void push(int c) {
  if (add[c]) {
    t[((c) << 1)].min += add[c];
    t[((c) << 1 | 1)].min += add[c];
    add[((c) << 1)] += add[c];
    add[((c) << 1 | 1)] += add[c];
    add[c] = 0;
  }
}
void modify(int c, int l, int r, int L, int R, int v) {
  if (L <= l && r <= R) {
    t[c].min += v;
    add[c] += v;
    return;
  }
  push(c);
  int m = (l + r) >> 1;
  if (L <= m) modify(((c) << 1), l, m, L, R, v);
  if (R > m) modify(((c) << 1 | 1), m + 1, r, L, R, v);
  t[c] = t[((c) << 1)] + t[((c) << 1 | 1)];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Reader r{cin};
  n = r;
  for (int i = 0; i < n; ++i) {
    int x = int(r) - 1, y = int(r) - 1;
    b[x] = y;
  }
  build(1, 0, n - 1);
  stack<int> u, d;
  long long c = 0;
  for (int i = 0; i < n; ++i) {
    while (!u.empty() && b[i] < b[u.top()]) {
      int r = u.top();
      u.pop();
      int l = u.empty() ? 0 : u.top() + 1;
      modify(1, 0, n - 1, l, r, b[r] - b[i]);
    }
    u.push(i);
    while (!d.empty() && b[i] > b[d.top()]) {
      int r = d.top();
      d.pop();
      int l = d.empty() ? 0 : d.top() + 1;
      modify(1, 0, n - 1, l, r, b[i] - b[r]);
    }
    d.push(i);
    c += t[1].count;
  }
  cout << c;
  return 0;
}
