Universal Cup Judging System

Universal Cup

Limite de temps : 1.5 s Limite de mémoire : 256 MB Points totaux : 100 Hackable ✓
Statistiques

You are given a directed graph with $n$ vertices and $m$ edges. Your task is to find the length of the longest sequence $a$ of distinct integers from $1$ to $n$ such that the following condition holds:

  • Let $l$ be the length of $a$. Then for all integers $i \in [2, l]$ and all vertices $v$ such that there exists an edge $(v, a_i)$, there exists an index $j < i$ such that $a_j = v$.

Input

First line contains $2$ integers $n, m$ ($2 \le n \le 3 \cdot 10^5, 0 \le m \le 3 \cdot 10^5$) — number of vertices and edges of the graph.

Next $m$ lines contain pairs of integers $u_i, v_i$ ($1 \le u_i, v_i \le n$) — edges of the graph. The graph is allowed to have loops and multiple edges.

Output

In the first line, output the maximal length of the correct sequence $l$.

Examples

Sample Input 1

5 0

Sample Output 1

5

Sample Input 2

5 5
1 2
3 2
2 4
3 5
5 3

Sample Output 2

5

Sample Input 3

3 3
1 1
2 2
3 3

Sample Output 3

1

Note

In the first sample case, one of the correct sequences could be $\{1, 2, 3, 4, 5\}$.

In the second sample case, one of the correct sequences could be $\{3, 5, 1, 2, 4\}$.

In the third sample case, one of the correct sequences could be $\{1\}$. Sequences $\{2\}$ and $\{3\}$ also satisfy the conditions of the problem.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#370EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.