Under construction

Still being built. Things may change

Popular Cows

Interview

Time limit1sMemory limit1024 MB

Summary
Given M directed pairs (A, B) meaning cow A thinks cow B is popular, count the cows that every other cow can reach through transitive popularity.
Level

Medium5 of 10

Topics
Graph, DFS, Topological sort
Solved
No attempts yet

Problem

Every cow's dream is to become the most popular cow in the herd. In a herd of NN (1≤N≤10,0001 \le N \le 10{,}000) cows, you are given up to MM (1≤M≤50,0001 \le M \le 50{,}000) ordered pairs of the form (A,B)(A, B). Each pair says that cow AA thinks cow BB is popular. Popularity is transitive: if AA thinks BB is popular and BB thinks CC is popular, then AA also thinks CC is popular, even if no pair in the input says so. Compute the number of cows that every other cow considers popular.

Input

The first line contains two space-separated integers NN and MM. Each of the next MM lines contains two space-separated integers AA and BB, meaning that cow AA thinks cow BB is popular.

Output

Print a single integer: the number of cows that are considered popular by every other cow.

Hint

Cow 3 is the only cow with high popularity.

Examples1

  1. Example 1

    Input
    3 3
    1 2
    2 1
    2 3
    
    Expected output
    1