#include <bits/stdc++.h>
using namespace std;
int n, ta, tb, x[3][20][100005 * 3], z[100005 * 3];
vector<int> y[100005 * 3];
long long ans;
void la(int c, int d) {
  x[2][c][d - 1] += x[2][c + 1][d / 2];
  x[0][c][d - 1] += x[2][c + 1][d / 2];
  if (((1 << (c)) * (d)) <= n) {
    x[2][c][d] += x[2][c + 1][d / 2];
    x[0][c][d] += x[2][c + 1][d / 2];
  }
  x[2][c + 1][d / 2] = 0;
}
void ca(int c, int d) {
  if (((1 << (c)) * (d)) <= n) {
    if (x[0][c][d - 1] > x[0][c][d]) {
      x[0][c + 1][d / 2] = x[0][c][d - 1];
      x[1][c + 1][d / 2] = x[1][c][d - 1];
    } else if (x[0][c][d] > x[0][c][d - 1]) {
      x[0][c + 1][d / 2] = x[0][c][d];
      x[1][c + 1][d / 2] = x[1][c][d];
    } else {
      x[0][c + 1][d / 2] = x[0][c][d - 1];
      x[1][c + 1][d / 2] = x[1][c][d - 1] + x[1][c][d];
    }
  } else {
    x[0][c + 1][d / 2] = x[0][c][d - 1];
    x[1][c + 1][d / 2] = x[1][c][d - 1];
  }
}
void f1(int a, int b, int c, int d) {
  if (((1 << (c)) * (d)) == a && ((1 << (c)) * (d + 1)) == b) {
    x[0][c][d] += ta;
    x[2][c][d] += ta;
    return;
  }
  c--;
  d = d * 2 + 1;
  if (x[2][c + 1][d / 2]) la(c, d);
  if (b <= ((1 << (c)) * (d)))
    f1(a, b, c, d - 1);
  else if (a >= ((1 << (c)) * (d)))
    f1(a, b, c, d);
  else {
    f1(a, ((1 << (c)) * (d)), c, d - 1);
    f1(((1 << (c)) * (d)), b, c, d);
  }
  ca(c, d);
}
int main() {
  scanf("%d", &n);
  for (int a = 0; a < n; a++) {
    scanf("%d%d", &ta, &tb);
    z[ta] = tb;
  }
  for (int a = 2; a <= n; a++)
    y[max(z[a], z[a - 1])].push_back(min(z[a], z[a - 1]));
  for (int a = 0; a < 20; a++)
    for (int b = 0; ((1 << (a)) * (b)) <= n; b++)
      x[1][a][b] = min(n + 1, ((1 << (a)) * (b + 1))) - ((1 << (a)) * (b));
  for (int a = 1; a <= n; a++) {
    ta = a;
    f1(a, a + 1, 19, 0);
    ta = 1;
    for (int b = 0; b < y[a].size(); b++) f1(1, y[a][b] + 1, 19, 0);
    if (x[0][19][0] == a) ans += x[1][19][0];
  }
  printf("%I64d\n", ans);
  return 0;
}
