Faulty Robot

Given a directed graph where at most one designated forced edge leaves each node, count the nodes where the robot can end up stopping if it breaks the forced rule at most once along the way.

Medium6GraphDFSSimulationBrute forceInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Alice has just finished programming her robot to explore a graph with nn nodes, labeled 1,2,,n1, 2, \dots, n, and mm directed edges. The robot starts at node 1.

A node may have several outgoing edges. Alice can designate one neighbor of a node as the target of a forced move. For example, node 5 may have outgoing edges to neighbors 1, 4, and 6, and if Alice designates the forced move to 4, the robot must go to 4 whenever it leaves 5.

A robot that works correctly always follows the forced move away from a node, and it stops when it reaches a node that has no forced move. This robot is buggy, so it might violate those rules and move to a randomly chosen neighbor of a node, whether or not that node had a designated forced move. Such a bug occurs at most once, and it might never happen.

Alice is stuck debugging the robot. Determine the nodes where the robot could stop and not move again.

Figures 1 and 2 show two sample graphs. A red arrow is an edge corresponding to a forced move, and a black arrow is any other edge. The circle around a node is red if the robot can stop there.

Figure 1: First sample graph.Figure 2: Second sample graph.

In the first graph the robot cycles forever through nodes 1, 5, and 4 if it makes no buggy move. A bug could make it jump from 1 to 2, and because that is the only buggy move it would never move on from there. It might also jump from 5 to 6 and then take a forced move to end at 7.

In the second graph there are no forced moves, so the robot stays at 1 when no bug occurs. It might also make a buggy move from 1 to either 2 or 3, after which it stops.

Input

The first line contains two integers nn and mm, the number of nodes and the number of edges. (1n10001 \le n \le 1000, 0m100000 \le m \le 10000)

Each of the next mm lines contains two integers aa and bb. (1a,bn1 \le |a|, b \le n, ab|a| \ne b) If a>0a > 0, there is a directed edge from node aa to node bb that is not forced. If a<0a < 0, there is a forced directed edge from node a-a to node bb. There are at most 900 forced moves. No two directed edges are the same, and no two forced moves start at the same node.

Output

Print the number of nodes at which the robot might come to a rest.