Networks

Given M directed edges on N ordered systems, repeatedly merge any pair of edges A->B and B->C into A->C; find how many edges remain at the end.

Medium6GraphGreedyMathCombinatoricsNo attempts yetTime limit1sMemory limit256 MB

Problem

The company has NN network systems S1,S2,,SNS_1, S_2, \dots, S_N and MM networks W1,W2,,WMW_1, W_2, \dots, W_M that connect them. Each system has a priority, and every network carries data only from a higher-priority system to a lower-priority one. That is, if a network carries data from SAS_A to SBS_B, then A<BA < B.

The networks have become too tangled, so the company decided to tidy them up. For three systems SAS_A, SBS_B, and SCS_C, if there is a network from SAS_A to SBS_B and a network from SBS_B to SCS_C, the two are merged into a single network from SAS_A to SCS_C. The two original networks disappear and one new network appears. Several networks may connect the same pair of systems, and each of them counts as a separate network.

The company repeats this simplification to reduce the number of networks as much as possible. Find the number of networks that remain.

Input

The first line contains NN and MM. (1N,M1061 \le N, M \le 10^6)

Each of the next MM lines contains two integers AiA_i and BiB_i, meaning that WiW_i carries data from SAiS_{A_i} to SBiS_{B_i}. (i=1,2,,Mi = 1, 2, \dots, M, 1Ai<BiN1 \le A_i < B_i \le N)

Output

Print the number of networks that remain after simplifying as much as possible.