This page is still under construction.

Parts of this page are still being built. What you see may change.

Evacuation

Time limit1sMemory limit128 MB

Summary
Find the minimum number of directed edges to remove so that no path of length at most three remains from node 1 to node n.
Level

Medium7 of 10

Topics
Graph, Shortest path, Minimum spanning tree, Implementation
Solved
No attempts yet

Problem

Because of a growing terrorist threat, the Agency for Defending Byteland (ADB) has decided to prepare a plan of action for the case of an attack. The agency's key concern is making sure that the king of Byteland can be evacuated quickly whenever a bombing takes place.

The royal palace stands next to one of the junctions in the capital of Byteland, and a shelter stands next to another junction; in case of danger the king must be moved there at once. The ADB has an exact road map of the capital, made up of junctions joined by one-way streets.

An evacuation route counts as quick if it uses at most three streets. When a bombing hits a street, that street becomes impassable for the royal convoy. The ADB wants to know the minimum number of streets that must be bombed so that the king is left with no quick evacuation route at all.

Input

The first line contains two integers nn and mm (2≤n≤10002 \le n \le 1000, 0≤m≤n(n−1)0 \le m \le n(n-1)): the number of junctions and the number of streets in the capital. Junctions are numbered from 11 to nn; the royal palace is next to junction 11 and the shelter is next to junction nn.

Each of the next mm lines contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n, ai≠bia_i \ne b_i), describing a one-way street that runs from junction aia_i to junction bib_i. For every ordered pair of junctions there is at most one street going from the first one to the second one.

Output

Print a single integer: the minimum number of streets that must be bombed so that the king has no evacuation route using at most three streets.

Hint

figure

In the picture above it is enough to bomb the streets 1→31 \to 3 and 3→53 \to 5 (shown crossed out) so that no quick evacuation route remains.

Examples1

  1. Example 1

    Input
    5 7
    1 2
    1 3
    2 3
    3 1
    3 4
    3 5
    4 5
    
    Expected output
    2