## https://yukicoder.me/problems/no/2796 MAX_INT = 10 ** 18 class BinaryIndexTree: """ フェニック木(BinaryIndexTree)の基本的な機能を実装したクラス """ def __init__(self, size): self.size = size self.array = [0] * (size + 1) def add(self, x, a): index = x while index <= self.size: self.array[index] += a index += index & (-index) def sum(self, x): index = x ans = 0 while index > 0: ans += self.array[index] index -= index & (-index) return ans def least_upper_bound(self, value): if self.sum(self.size) < value: return -1 elif value <= 0: return 0 m = 1 while m < self.size: m *= 2 k = 0 k_sum = 0 while m > 0: k0 = k + m if k0 < self.size: if k_sum + self.array[k0] < value: k_sum += self.array[k0] k += m m //= 2 if k < self.size: return k + 1 else: return -1 def main(): N = int(input()) rR = [] for _ in range(N): r, R = map(int, input().split()) rR.append((r, R)) rR.sort(key=lambda x : x[0], reverse=True) # 座標圧縮 r_set = set() for r, R in rR: r_set.add(r) r_set.add(R) r_list = list(r_set) r_list.sort() r_map = {} for i, r in enumerate(r_list): r_map[r] = i + 1 # BinaryIndexTree bit = BinaryIndexTree(len(r_list)) for r, R in rR: x = bit.sum(bit.size) - bit.sum(r_map[R] - 1) if x == 0: bit.add(r_map[r], 1) else: r_ = bit.least_upper_bound(bit.sum(r_map[R] - 1) + 1) bit.add(r_, -1) bit.add(r_map[r], 1) a = bit.sum(bit.size) print(a - 1) if __name__ == "__main__": main()