Explosion at Cafebazaar

A directed multigraph alternates send and receive rounds from an initial 1-bit packet; count the starting switches whose bit makes some buffer grow without bound.

Medium7GraphDFSSimulationImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

You are an engineer at Cafebazaar and your work is to analyze networks and how they behave. Your colleagues designed a network of nn switches with mm directed links that connect pairs of these switches. Each switch has one buffer where it stores data, and it has two modes, sending mode and receiving mode. A switch in sending mode sends the data stored in its buffer to every one of its outgoing links at the same time, and clears its buffer at the end. A switch in receiving mode concatenates the data from all of its incoming links and stores the result in its buffer, so the length of the data in its buffer then equals the sum of the lengths of the data on the incoming links.

At time t=0t = 0 every switch is in sending mode with an empty buffer, except switch ii, whose buffer holds a package of 1 bit. Every switch changes its mode after each second, so at time t=1t = 1 all switches turn to receiving mode, at time t=2t = 2 they turn to sending mode, and so on. Switch ii is called explosive if the maximum length of the data stored in the buffers of the switches is not bounded as tt goes to infinity.

Compute the number of explosive switches in the network.

Input

The input holds several test cases. The first line of each test case has two space separated integers nn and mm, where nn is the number of switches and mm is the number of directed links (1n,m500001 \le n, m \le 50000). Each of the next mm lines has two space separated integers uu and vv, meaning a directed link from switch uu to switch vv (1u,vn1 \le u, v \le n, uvu \ne v). The same pair (u,v)(u, v) may appear several times, and then it counts as that many separate links.

The input ends with a line containing 0 0, which you must not process.

Output

For each test case, print one line with the number of explosive switches.