from bisect import bisect_left, bisect_right N = int(input()) LR = [tuple(map(int, input().split())) for _ in range(N)] LR.sort(key=lambda x: x[1], reverse=True) Q = [] while LR: l, r = LR.pop() j = bisect_right(Q, l) if j == 0: Q.append(r) else: Q[j-1] = r ans = len(Q) - 1 print(ans)