#include <bits/stdc++.h>
using namespace std;
namespace IO {
inline char fch() {
  static char s[1 << 16], *S, *T;
  return (S == T) && (T = (S = s) + fread(s, 1, 1 << 16, stdin), S == T) ? EOF
                                                                         : *S++;
}
template <typename T>
void read(T &x) {
  x = 0;
  int f(1);
  char c = fch();
  for (; !isdigit(c); c = fch())
    if (c == '-') f = -1;
  for (; isdigit(c); c = fch()) x = x * 10 + c - '0';
  x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x);
  read(args...);
}
}  // namespace IO
using namespace IO;
const int maxn = 300005;
const int inf = 0x3f3f3f3f;
int n, L[maxn], R[maxn], Rt, VL;
struct fhq_treap {
  int ch[maxn][2], sz[maxn];
  int mx[maxn], vl[maxn], mn[maxn];
  int rk[maxn], tot, fg[maxn];
  inline void pushup(int x) {
    sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
    mx[x] = max(max(vl[x], mx[ch[x][0]]), mx[ch[x][1]]);
    mn[x] = min(min(vl[x], mn[ch[x][0]]), mn[ch[x][1]]);
  }
  inline void update(int x, int f) {
    fg[x] += f;
    vl[x] += f;
    mx[x] += f;
    mn[x] += f;
  }
  inline void pushdown(int x) {
    if (fg[x]) {
      if (ch[x][0]) update(ch[x][0], fg[x]);
      if (ch[x][1]) update(ch[x][1], fg[x]);
    }
    fg[x] = 0;
  }
  int merge(int x, int y) {
    if (!x || !y) return x + y;
    pushdown(x);
    pushdown(y);
    if (rk[x] < rk[y]) {
      ch[x][1] = merge(ch[x][1], y);
      pushup(x);
      return x;
    } else {
      ch[y][0] = merge(x, ch[y][0]);
      pushup(y);
      return y;
    }
  }
  void split(int now, int ks, int &x, int &y) {
    if (ks <= 0) return x = 0, y = now, void();
    if (!now) return x = y = 0, void();
    pushdown(now);
    if (sz[ch[now][0]] + 1 <= ks)
      x = now, split(ch[now][1], ks - sz[ch[now][0]] - 1, ch[now][1], y);
    else
      y = now, split(ch[now][0], ks, x, ch[now][0]);
    pushup(now);
  }
  int Find_mx(int V) {
    int nw = Rt, TSZ = 0;
    while (1) {
      pushdown(nw);
      if (ch[nw][0] && mx[ch[nw][0]] >= V) {
        nw = ch[nw][0];
        continue;
      } else if (vl[nw] >= V)
        return VL = V, sz[ch[nw][0]] + 1 + TSZ;
      else
        TSZ += sz[ch[nw][0]] + 1, nw = ch[nw][1];
    }
  }
  int Find_mn(int V) {
    int nw = Rt, TSZ = 0;
    while (1) {
      pushdown(nw);
      if (ch[nw][1] && mn[ch[nw][1]] <= V) {
        TSZ += sz[ch[nw][0]] + 1, nw = ch[nw][1];
        continue;
      } else if (vl[nw] <= V)
        return TSZ + sz[ch[nw][0]] + 1;
      else
        nw = ch[nw][0];
    }
  }
  void Move(int l, int r) {
    if (r == n + 1) --r;
    if (l > r) {
      int A = 0, B = 0, C = 0;
      split(Rt, l - 1, A, B);
      split(B, 1, B, C);
      vl[B] = mx[B] = mn[B] = VL;
      Rt = merge(A, B);
      Rt = merge(Rt, C);
      return;
    }
    int A = 0, B = 0, C = 0, D = 0;
    split(Rt, l - 1, A, B);
    split(B, r - l + 1, B, C);
    split(C, 1, C, D);
    mx[C] = vl[C] = mn[C] = VL;
    update(B, 1);
    Rt = merge(A, C);
    Rt = merge(Rt, B);
    Rt = merge(Rt, D);
  }
  int nwnode(int ps) {
    ++tot;
    rk[tot] = rand();
    ch[tot][0] = ch[tot][1] = 0;
    sz[tot] = 1;
    mx[tot] = mn[tot] = vl[tot] = (ps == 0) ? 0 : inf;
    return tot;
  }
} fhq;
int main() {
  srand(161102251);
  read(n);
  Rt = fhq.nwnode(0);
  fhq.mn[0] = inf;
  for (int i(1); i <= n; ++i) {
    read(L[i], R[i]);
    Rt = fhq.merge(Rt, fhq.nwnode(i));
  }
  for (int i(1); i <= n; ++i) {
    int tL = L[i], tR = R[i] - 1;
    tL = fhq.Find_mx(tL);
    tR = fhq.Find_mn(tR);
    fhq.Move(tL, tR);
  }
  int x = fhq.Find_mn(inf - 100);
  cout << x - 1 << endl;
  return 0;
}
