#c22607. 最小点数负环

最小点数负环

Minimum-Vertex Negative Cycle

Problem Description

Given a directed weighted simple graph with nn vertices and mm edges, with vertices numbered from 11 to nn, find a cycle with the smallest number of vertices such that the sum of edge weights on the cycle is negative.

Input Format

The first line contains two integers n,mn, m, the number of vertices and edges.

The next mm lines each contain three integers u,v,wu, v, w, meaning there is a directed edge of weight ww from vertex uu to vertex vv.

Output Format

Output one line with an integer: the number of vertices on the smallest negative cycle. If no negative cycle exists, output -1.

Sample Input

3 6
1 2 -2
2 1 1
2 3 -10
3 2 10
3 1 -10
1 3 10

Sample Output

2

Constraints

For 100%100\% of the data: 1≤n≤3001 \leq n \leq 300, 0≤m≤n(n−1)0 \leq m \leq n(n-1), 1≤u,v≤n1 \leq u, v \leq n, −104≤w≤104-10^4 \leq w \leq 10^4.