#include <bits/stdc++.h>   using namespace std;   inline char nc() {     static char buf[100000], *l = buf, *r = buf;     return l == r && (r = (l = buf) + fread(buf, 1, 100000, stdin), l == r)                ? EOF                : *l++;   }   template <class T>   void read(T &x) {     x = 0;     int f = 1, ch = nc();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = nc();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 -  0  + ch;       ch = nc();     }     x *= f;   }   const int inf = 1e9;   const int maxn = 3e5 + 50;   int n;   int a[maxn];   int b[maxn];   long long an;   namespace seg {   const int maxnode = maxn << 2;   int mn[maxnode];   int cnt[maxnode];   int tag[maxnode];   inline void change(int u, int d) {     mn[u] += d;     tag[u] += d;   }   inline void pushup(int u) {     mn[u] = min(mn[u << 1], mn[u << 1 | 1]);     cnt[u] = 0;     if (mn[u] == mn[u << 1]) cnt[u] += cnt[u << 1];     if (mn[u] == mn[u << 1 | 1]) cnt[u] += cnt[u << 1 | 1];   }   inline void pushdown(int u) {     if (tag[u]) {       change(u << 1, tag[u]);       change(u << 1 | 1, tag[u]);       tag[u] = 0;     }   }   void build(int u, int l, int r) {     tag[u] = 0;     if (l == r) {       mn[u] = -l;       cnt[u] = 1;       return;     }     int mid = (l + r) >> 1;     build(u << 1, l, mid);     build(u << 1 | 1, mid + 1, r);     pushup(u);   }   void update(int u, int l, int r, int ql, int qr, int qv) {     if (l == ql && r == qr) {       change(u, qv);       return;     }     pushdown(u);     int mid = (l + r) >> 1;     if (qr <= mid)       update(u << 1, l, mid, ql, qr, qv);     else if (ql > mid)       update(u << 1 | 1, mid + 1, r, ql, qr, qv);     else {       update(u << 1, l, mid, ql, mid, qv);       update(u << 1 | 1, mid + 1, r, mid + 1, qr, qv);     }     pushup(u);   }   }  // namespace seg   void divide(int l, int r) {     static int mn[maxn];     int hd0 = 1, tl0 = 0;     static int mx[maxn];     int hd1 = 1, tl1 = 0;     if (l == r) {       return;     }     int mid = (l + r) >> 1;     divide(l, mid);     divide(mid + 1, r);     int L = r - mid;     seg::build(1, 1, L);     for (int i = 1; i <= L; ++i) {       b[i] = a[mid + i];       if (hd0 > tl0 || b[i] < b[mn[tl0]]) mn[++tl0] = i;       if (hd1 > tl1 || b[i] > b[mx[tl1]]) mx[++tl1] = i;     }     mn[++tl0] = L + 1;     mx[++tl1] = L + 1;     for (int i = hd0; i < tl0; ++i) {       seg::update(1, 1, L, mn[i], mn[i + 1] - 1, -b[mn[i]]);     }     for (int i = hd1; i < tl1; ++i) {       seg::update(1, 1, L, mx[i], mx[i + 1] - 1, b[mx[i]]);     }     int Mx = -inf, Mn = inf;     for (int i = mid; i >= l; --i) {       seg::update(1, 1, L, 1, L, -1);       if (a[i] < Mn) {         if (hd0 != 1) seg::update(1, 1, L, 1, mn[hd0] - 1, Mn);         Mn = a[i];         while (hd0 < tl0 && Mn < b[mn[hd0]]) {           seg::update(1, 1, L, mn[hd0], mn[hd0 + 1] - 1, b[mn[hd0]]), ++hd0;         }         if (hd0 != 1) seg::update(1, 1, L, 1, mn[hd0] - 1, -Mn);       }       if (a[i] > Mx) {         if (hd1 != 1) seg::update(1, 1, L, 1, mx[hd1] - 1, -Mx);         Mx = a[i];         while (hd1 < tl1 && Mx > b[mx[hd1]]) {           seg::update(1, 1, L, mx[hd1], mx[hd1 + 1] - 1, -b[mx[hd1]]), ++hd1;         }         if (hd1 != 1) seg::update(1, 1, L, 1, mx[hd1] - 1, Mx);       }       if (seg::mn[1] == -1) an += seg::cnt[1];     }   }   int main() {     read(n);     for (int i = 1; i <= n; ++i) {       int r, c;       read(r), read(c);       a[r] = c;     }     an = n;     divide(1, n);     printf( %lld n , an);     return 0;   }