class SegmentTree(object): def __init__(self, A, dot, unit): n = 1 << (len(A) - 1).bit_length() tree = [unit] * (2 * n) for i, v in enumerate(A): tree[i + n] = v for i in range(n - 1, 0, -1): tree[i] = dot(tree[i << 1], tree[i << 1 | 1]) self._n = n self._tree = tree self._dot = dot self._unit = unit def __getitem__(self, i): return self._tree[i + self._n] def update(self, i, v): i += self._n self._tree[i] = v while i != 1: i >>= 1 self._tree[i] = self._dot(self._tree[i << 1], self._tree[i << 1 | 1]) def add(self, i, v): self.update(i, self[i] + v) def sum(self, l, r): #これで[l,r)から取り出す。 l += self._n r += self._n l_val = r_val = self._unit while l < r: if l & 1: l_val = self._dot(l_val, self._tree[l]) l += 1 if r & 1: r -= 1 r_val = self._dot(self._tree[r], r_val) l >>= 1 r >>= 1 return self._dot(l_val, r_val) def segfunc(x,y): return x*y def main(): N,M = map(int,input().split()) Z = [] S = set([]) for _ in range(M): l,r = map(int,input().split()) l-=1;r-=1 #0-index Z.append((l,r)) S.add(l) S.add(r) Z.sort(key = lambda x: x[1]) val = 0 dic = {} S = list(S); S.sort() for s in S: dic[s] = val val += 1 NN = len(S) A = [1]*NN Tree = SegmentTree(A,segfunc,1) for i in range(M): l,r = Z[i] nl = dic[l] nr = dic[r] if Tree.sum(nl,nr+1) == 1: Tree.update(nr,0) ans = N for i in range(NN): if Tree.__getitem__(i) == 0: ans -= 1 #print(dic) print(ans) if __name__ == '__main__': main()