This page is still under construction.

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

Grass Cownoisseur

Time limit1sMemory limit256 MB

Summary
Starting from field 1 and returning to it, visit the most distinct fields while traveling at most one directed path backwards.
Level

Medium7 of 10

Topics
Graph, Topological sort, Dynamic programming
Solved
No attempts yet

Problem

Farmer John installed one-way cow paths all over his farm to manage how his cows graze. The farm has NN fields numbered 11 through NN, and each path connects a pair of fields. If a path runs from field XX to field YY, cows may travel from XX to YY but not from YY to XX.

Bessie the cow wants to eat grass in as many fields as possible. She starts her day in field 11, walks through a sequence of fields, and returns to field 11 at the end of the day. She eats the grass of a field only the first time she is there, so she tries to maximize the number of distinct fields on her route.

The one-way rule cuts down how many fields Bessie reaches in one day. She wonders how much grass she gets if she breaks the rule and follows one path in the wrong direction. Compute the largest number of distinct fields on a route that starts and ends at field 11 when she may follow at most one path in the wrong direction. She travels backwards at most once per day, so she cannot take the same path backwards twice either.

Input

The first line contains the number of fields NN and the number of one-way paths MM. (1≤N,M≤100,0001 \le N, M \le 100{,}000)

Each of the next MM lines describes one path with two distinct field numbers XX and YY, meaning there is a path from XX to YY. The same path never appears more than once.

Output

Print one line with the maximum number of distinct fields Bessie visits on a route that starts and ends at field 11 and follows at most one path in the wrong direction.

Hint

Here is a drawing of the farm in the first example.

v---3-->6
7   |\  |
^\  v \ |
| \ 1  \|
|  \|   v
|   v   5
4<--2---^

Bessie can walk 1,2,4,7,2,5,3,11, 2, 4, 7, 2, 5, 3, 1 by traveling backwards on the path between 55 and 33. Once she reaches field 33 she cannot get to field 66 without following another path backwards.

Examples2

  1. Example 1

    Input
    7 10
    1 2
    3 1
    2 5
    2 4
    3 7
    3 5
    3 6
    6 5
    7 2
    4 7
    
    Expected output
    6
    
  2. Example 2

    Input
    2 1
    1 2
    
    Expected output
    2