from itertools import chain import sys import os import inspect from operator import itemgetter input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) if os.getenv("TKTKLOCAL", False): def debug(*arg, sep=" ", end="\n"): print(*arg, sep=sep, end=end, file=sys.stderr) def debug_indent(*arg, sep=" ", end="\n", indent=" "): frame = inspect.currentframe().f_back par_func = inspect.getframeinfo(frame).function if par_func == "": debug(*arg, sep=sep, end=end) return frame_stack = inspect.stack() if len(frame_stack) > 30: return depth = sum(f.function == par_func for f in frame_stack) debug(indent * (depth - 1), end="") debug(*arg, sep=sep, end=end) else: def debug(*arg, **kwarg): pass def debug_indent(*arg, **kwarg): pass class FenwickTree(object): def __init__(self, n): self.n = n self.log = n.bit_length() self.data = [0] * n def _sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s def add(self, p, x): """ a[p] += xを行う""" p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): """a[l] + a[l+1] + .. + a[r-1]を返す""" return self._sum(r) - self._sum(l) def lower_bound(self, x): """a[0] + a[1] + .. a[i] >= x となる最小のiを返す""" if x <= 0: return -1 i = 0 k = 1 << self.log while k: if i + k <= self.n and self.data[i + k - 1] < x: x -= self.data[i + k - 1] i += k k >>= 1 return i def __repr__(self): res = [self.sum(i, i+1) for i in range(self.n)] return " ".join(map(str, res)) def main(): N = int(input()) M = list(tuple(map(int, input().split())) for _ in range(N)) M.sort(key=itemgetter(1, 0)) R = sorted(set(chain(*M))) rtoi = {r: i for i, r in enumerate(R)} bit = FenwickTree(len(rtoi) + 5) cnt = 0 for s, t in M: s = rtoi[s] t = rtoi[t] f = bit.sum(0, s+1) if f == 0: cnt += 1 bit.add(t, 1) else: i = bit.lower_bound(f) bit.add(i, -1) bit.add(t, 1) print(cnt - 1) main()