In this problem you will meet the simplified model of game Pudding Monsters.

An important process in developing any game is creating levels. A game field in Pudding Monsters is an n × n rectangular grid, n of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?).

<image>

Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects. 

A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large n × n map, you can choose in it a smaller k × k square part, containing exactly k monsters and suggest it as a simplified version of the original map.

You wonder how many ways there are to choose in the initial map a k × k (1 ≤ k ≤ n) square fragment, containing exactly k pudding monsters. Calculate this number.

Input

The first line contains a single integer n (1 ≤ n ≤ 3 × 105) — the size of the initial field.

Next n lines contain the coordinates of the cells initially containing monsters. The i-th of the next lines contains two numbers ri, ci (1 ≤ ri, ci ≤ n) — the row number and the column number of the cell that initially contains the i-th monster.

It is guaranteed that all ri are distinct numbers and all ci are distinct numbers.

Output

Print the number of distinct square fragments of the original field that can form a new map.

Examples

Input

5
1 1
4 3
3 2
2 4
5 5


Output

10
#include <bits/stdc++.h>
const int N = 300005;
using namespace std;
int n, x, y, a[N], i, cnt;
long long ans;
struct T {
  int l, r, ad, mn, ti;
  T *ls, *rs;
  void build(int, int);
  inline void pls(int v) { ad += v, mn += v; }
  void deb() {
    cerr << '[' << l << ',' << r << ']' << mn << '/' << ti << '(' << ad << endl;
    if (l < r) ls->deb(), rs->deb();
  }
  void mdf(int v) {
    if (l > y || r < x) return;
    if (l >= x && r <= y)
      pls(v);
    else {
      if (ad) ls->pls(ad), rs->pls(ad), ad = 0;
      ls->mdf(v), rs->mdf(v);
      mn = min(ls->mn, rs->mn), ti = 0;
      if (mn == ls->mn) ti += ls->ti;
      if (mn == rs->mn) ti += rs->ti;
    }
  }
} b[N * 2], *rt;
void T::build(int L, int R) {
  l = mn = L, r = R, ti = 1;
  if (l < r) {
    int mi = l + r >> 1;
    (ls = &b[cnt++])->build(l, mi);
    (rs = &b[cnt++])->build(mi + 1, r);
  }
}
struct sta {
  int sx[N], sy[N], t;
  inline void ins(int v) {
    int ls = x = y = i;
    rt->mdf(v);
    for (; t && sy[t] < v; t--)
      x = sx[t], y = ls - 1, rt->mdf(v - sy[t]), ls = x;
    sx[++t] = ls, sy[t] = v;
  }
} Mx, Mn;
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d%d", &x, &y), a[x] = y;
  (rt = &b[cnt++])->build(1, n);
  for (i = 1; i <= n; i++) Mx.ins(a[i]), Mn.ins(-a[i]), ans += rt->ti;
  printf("%lld", ans);
}
