#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn], lazy[maxn * 4], mn[maxn * 4], num[maxn * 4];
void shift(int v) {
  lazy[v * 2] += lazy[v];
  lazy[v * 2 + 1] += lazy[v];
  mn[v * 2] += lazy[v];
  mn[v * 2 + 1] += lazy[v];
  lazy[v] = 0;
}
void add(int v, int l, int r, int b, int e, int val) {
  if (e <= l || r <= b) return;
  if (b <= l && r <= e) {
    lazy[v] += val;
    mn[v] += val;
    return;
  }
  int mid = (l + r) / 2;
  shift(v);
  add(v * 2, l, mid, b, e, val);
  add(v * 2 + 1, mid, r, b, e, val);
  mn[v] = min(mn[v * 2], mn[v * 2 + 1]);
  num[v] = num[v * 2];
  if (mn[v * 2] == mn[v * 2 + 1]) num[v] += num[v * 2 + 1];
  if (mn[v * 2 + 1] < mn[v * 2]) num[v] = num[v * 2 + 1];
}
pair<int, int> query(int v, int l, int r, int b, int e) {
  if (e <= l || r <= b) return {1e9, -1};
  if (b <= l && r <= e) return {mn[v], num[v]};
  int mid = (l + r) / 2;
  shift(v);
  pair<int, int> left, right, now;
  left = query(v * 2, l, mid, b, e);
  right = query(v * 2 + 1, mid, r, b, e);
  if (left.second == -1) return right;
  if (right.second == -1) return left;
  now.first = min(left.first, right.first);
  now.second = left.second;
  if (left.first == right.first) now.second += right.second;
  if (left.first > right.first) now.second = right.second;
  return now;
}
void build(int v, int l, int r, int x) {
  if (r - l == 1) {
    num[v] = 1;
    return;
  }
  int mid = (l + r) / 2;
  if (x < mid)
    build(v * 2, l, mid, x);
  else
    build(v * 2 + 1, mid, r, x);
  mn[v] = r - l;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    cin >> a[x];
    a[x]--;
    build(1, 0, n, i);
  }
  stack<int> st, st2;
  st.push(-1);
  st2.push(-1);
  long long res = 0;
  for (int i = 0; i < n; i++) {
    add(1, 0, n, i, i + 1, i + a[i] - a[i]);
    while (st.top() > -1 && st.size() && a[st.top()] < a[i]) {
      int last = st.top();
      st.pop();
      add(1, 0, n, st.top() + 1, last + 1, a[i] - a[last]);
    }
    st.push(i);
    while (st2.top() > -1 && st2.size() && a[st2.top()] > a[i]) {
      int last = st2.top();
      st2.pop();
      add(1, 0, n, st2.top() + 1, last + 1, -a[i] + a[last]);
    }
    st2.push(i);
    res += query(1, 0, n, 0, i + 1).second;
  }
  cout << res << endl;
  return 0;
}
