Cake delivery

Given a directed graph where every village must be reachable from village 1, find the minimum number of walks starting at village 1 that together cover all vertices.

Medium7GraphDynamic programmingGreedyShortest pathNo attempts yetTime limit1sMemory limit64 MB

Problem

Mirko makes the best cake in his village, a grapefruit cheesecake. To advertise the recipe he decided to give at least one cake to each of the NN villages in his county. He hires several traveling salesmen for the job. A salesman carries as many cakes as he wants and hands them out while moving along the one way roads that connect the villages.

Every salesman starts in Mirko's village, which is village 1. Mirko picks the route of every salesman he hires. A route is any sequence of roads joined end to end, and it may pass through the same village more than once. A salesman is allowed to stay put, and such a salesman delivers only to village 1.

What is the smallest number of salesmen Mirko has to hire so that every village gets a cake?

Input

The first line contains the number of villages NN and the number of roads EE (1N5001 \le N \le 500, 1E500001 \le E \le 50000).

Each of the next EE lines contains two integers AA and BB (1A,BN1 \le A, B \le N). This means there is a one way road from village AA to village BB, so a salesman can go directly from village AA to village BB.

The same road may be given more than once, and AA and BB may be equal.

Output

Print the smallest number of salesmen Mirko has to hire. The input is always such that a way to give every village a cake exists.