Minimum Chain Cover

Given a DAG, find the minimum number of vertex-disjoint directed paths that together cover every vertex.

Hard8GraphDynamic programmingShortest pathNo attempts yetTime limit2sMemory limit512 MB

Problem

In a graph, a chain is a sequence of vertices in which every pair of neighbouring vertices is joined by an edge. The same vertex may appear more than once, and a single vertex is already a chain.

You are given a directed graph with no cycles. Find the smallest number of chains needed to cover every vertex.

Two different chains may not share a vertex. A sequence v1,v2,,vkv_1, v_2, \dots, v_k is a chain only if there is an edge from viv_i to vi+1v_{i+1} for every ii, so edges cannot be traversed against their direction.

Input

The first line contains the number of vertices NN (1N100001 \le N \le 10000) and the number of edges MM (0M1000000 \le M \le 100000). The vertices are numbered from 11 to NN.

Each of the next MM lines contains two integers uu and vv, meaning there is an edge from uu to vv. The same edge may be given more than once.

The given graph contains no cycle.

Output

Print the minimum number of chains needed to cover every vertex, on one line.

Hint

The first example is covered by the two chains 27362 \to 7 \to 3 \to 6 and 1541 \to 5 \to 4, which together hold all seven vertices.