#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int n, lmn[N], lmx[N], p[N], lazy[4 * N];
struct node {
  int mn, sz;
  node() {
    mn = 0;
    sz = 0;
  }
} seg[4 * N];
node merge(node a, node b) {
  node ret = node();
  ret.mn = min(a.mn, b.mn);
  if (a.mn < b.mn)
    ret.sz = a.sz;
  else if (a.mn > b.mn)
    ret.sz = b.sz;
  else if (a.mn == b.mn)
    ret.sz = a.sz + b.sz;
  return ret;
}
void build(int s = 0, int e = n, int id = 1) {
  if (e - s == 1) {
    seg[id] = node();
    seg[id].mn = 0;
    seg[id].sz = 1;
    return;
  }
  int mid = (s + e) / 2;
  build(s, mid, 2 * id);
  build(mid, e, 2 * id + 1);
  seg[id] = merge(seg[2 * id], seg[2 * id + 1]);
}
void shift(int id) {
  seg[2 * id].mn += lazy[id];
  seg[2 * id + 1].mn += lazy[id];
  lazy[2 * id] += lazy[id];
  lazy[2 * id + 1] += lazy[id];
  lazy[id] = 0;
}
void upd(int l, int r, int val, int s = 0, int e = n, int id = 1) {
  if (e <= l || r <= s) return;
  if (l <= s && e <= r) {
    seg[id].mn += val;
    lazy[id] += val;
    return;
  }
  int mid = (s + e) / 2;
  shift(id);
  upd(l, r, val, s, mid, 2 * id);
  upd(l, r, val, mid, e, 2 * id + 1);
  seg[id] = merge(seg[2 * id], seg[2 * id + 1]);
}
node get(int l, int r, int s = 0, int e = n, int id = 1) {
  if (e <= l || r <= s) {
    node cur;
    cur.mn = 1e9;
    cur.sz = -1;
    return cur;
  }
  if (l <= s && e <= r) return seg[id];
  shift(id);
  int mid = (s + e) / 2;
  return merge(get(l, r, s, mid, 2 * id), get(l, r, mid, e, 2 * id + 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  build();
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[a] = b;
  }
  stack<int> smn, smx;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int las = i;
    while (smn.size() && p[smn.top()] > p[i]) {
      upd(lmn[smn.top()], las, p[smn.top()] - p[i]);
      las = smn.top();
      smn.pop();
    }
    if (smn.size()) lmn[i] = smn.top() + 1;
    las = i;
    while (smx.size() && p[smx.top()] < p[i]) {
      upd(lmx[smx.top()], las, p[i] - p[smx.top()]);
      las = smx.top();
      smx.pop();
    }
    if (smx.size()) lmx[i] = smx.top() + 1;
    smn.push(i);
    smx.push(i);
    upd(i, i + 1, i);
    ans += get(0, i + 1).sz;
  }
  cout << ans << "\n";
  return 0;
}
