#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
vector<int> v[N];
int n, a[N], b[N], x;
struct Tree {
  int l, r, flag;
  pair<int, int> num;
} T[N * 4];
pair<int, int> hb(pair<int, int> x, pair<int, int> y) {
  if (x.first == y.first) return make_pair(x.first, x.second + y.second);
  if (x.first > y.first) return x;
  return y;
}
void build(int x, int l, int r) {
  T[x].l = l;
  T[x].r = r;
  if (l == r) {
    T[x].num = make_pair(l, 1);
    return;
  }
  int mid = (l + r) / 2;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
  T[x].num = hb(T[x * 2].num, T[x * 2 + 1].num);
}
void down(int x) {
  T[x * 2].num.first += T[x].flag;
  T[x * 2 + 1].num.first += T[x].flag;
  T[x * 2].flag += T[x].flag;
  T[x * 2 + 1].flag += T[x].flag;
  T[x].flag = 0;
}
void insert(int x, int l, int r) {
  if (T[x].l > r || l > T[x].r) return;
  if (T[x].l >= l && T[x].r <= r) {
    T[x].flag++;
    T[x].num.first++;
    return;
  }
  down(x);
  insert(x * 2, l, r);
  insert(x * 2 + 1, l, r);
  T[x].num = hb(T[x * 2].num, T[x * 2 + 1].num);
}
pair<int, int> find(int x, int l, int r) {
  if (T[x].l > r || l > T[x].r) return make_pair(-1, 0);
  if (T[x].l >= l && T[x].r <= r) return T[x].num;
  down(x);
  return hb(find(x * 2, l, r), find(x * 2 + 1, l, r));
}
int main() {
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    scanf("%d", &a[x]);
    b[a[x]] = x;
  }
  for (int i = 1; i < n; i++)
    v[max(b[i], b[i + 1])].push_back(min(b[i], b[i + 1]));
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < v[i].size(); j++) insert(1, 1, v[i][j]);
    pair<int, int> now = find(1, 1, i);
    if (now.first == i) ans += now.second;
  }
  printf("%lld\n", ans);
}
