Problem: You are given a directed acyclic graph, consisting of vertices and edges. The vertices are numbered from to . There are no multiple edges and self-loops.
Let be the number of incoming edges (indegree) and be the number of outgoing edges (outdegree) of vertex .
You are asked to remove some edges from the graph. Let the new degrees be and .
You are only allowed to remove the edges if the following conditions hold for every vertex :
- or ;
- or .
Let's call a set of vertices cute if for each pair of vertices and () such that and , there exists a path either from to or from to over the non-removed edges.
What is the maximum possible size of a cute set after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to ?
Input Format: The first line contains two integers and (; ) — the number of vertices and the number of edges of the graph.
Each of the next lines contains two integers and (; ) — the description of an edge.
The given edges form a valid directed acyclic graph. There are no multiple edges.
Output Format: Print a single integer — the maximum possible size of a cute set after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to .
Note: In the first example, you can remove edges and . , . , . You can see that for all the conditions hold. The maximum cute set is formed by vertices and . They are still connected directly by an edge, so there is a path between them.
In the second example, there are no edges. Since all and are equal to , leaving a graph with zero edges is allowed. There are cute sets, each contains a single vertex. Thus, the maximum size is .
In the third example, you can remove edges , , and . The maximum cute set will be . You can remove edge as well, and the answer won't change.
Here is the picture of the graph from the third example: