#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
int n, a[N], best[4 * N], cnt[4 * N], lzy[4 * N];
long long res = 0;
map<int, int> mp;
stack<pair<int, int> > s[10];
void build(int b = 0, int e = n, int ind = 1) {
  cnt[ind] = e - b;
  if (e - b > 1) {
    int mid = (b + e) / 2;
    build(b, mid, ind * 2);
    build(mid, e, ind * 2 + 1);
  }
}
void shift(int b, int e, int ind) {
  if (!lzy[ind]) return;
  int tmp = lzy[ind];
  best[ind * 2] += tmp;
  best[ind * 2 + 1] += tmp;
  lzy[ind * 2] += tmp;
  lzy[ind * 2 + 1] += tmp;
  lzy[ind] = 0;
}
void update(int l, int r, int val, int b = 0, int e = n, int ind = 1) {
  if (l <= b && e <= r) {
    best[ind] += val;
    lzy[ind] += val;
    return;
  }
  shift(b, e, ind);
  int mid = (b + e) / 2;
  if (l < mid) update(l, r, val, b, mid, ind * 2);
  if (r > mid) update(l, r, val, mid, e, ind * 2 + 1);
  if (best[ind * 2] == best[ind * 2 + 1])
    cnt[ind] = cnt[ind * 2] + cnt[ind * 2 + 1];
  else
    cnt[ind] =
        (best[ind * 2] > best[ind * 2 + 1] ? cnt[ind * 2] : cnt[ind * 2 + 1]);
  best[ind] = max(best[ind * 2], best[ind * 2 + 1]);
}
bool comp(bool f, int x, int y) {
  if (!f) return x > y;
  return x < y;
}
int bamosama(bool f, int bef, int aft) {
  if (!f) return -bef + aft;
  return +bef - aft;
}
void dealwithit(bool b, int i) {
  while (!s[b].empty() && comp(b, s[b].top().first, a[i])) {
    pair<int, int> toff = s[b].top();
    s[b].pop();
    int puff = n;
    if (!s[b].empty()) puff = s[b].top().second;
    update(toff.second, puff, bamosama(b, toff.first, a[i]));
  }
  s[b].push({a[i], i});
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r, c;
    cin >> r >> c;
    mp[r] = c;
  }
  for (int i = 0; i < n; i++) a[i] = mp[i + 1];
  build();
  for (int i = n - 1; i >= 0; i--) {
    if (i < n - 1) update(i + 1, n, 1);
    dealwithit(0, i);
    dealwithit(1, i);
    if (!best[1]) res += cnt[1] - i;
  }
  cout << res << endl;
}
