Codeforces Testing Round 4
B
Smile House
Codeforces
2500
原题
← A→
翻译中…

A smile house is created to raise the mood. It has $*n*$ rooms. Some of the rooms are connected by doors. For each two rooms (number $*i*$ and $*j*$), which are connected by a door, Petya knows their value $*c*_{*ij*}$ — the value which is being added to his mood when he moves from room $*i*$ to room $*j*$.

翻译中…

Petya wondered whether he can raise his mood infinitely, moving along some cycle? And if he can, then what minimum number of rooms he will need to visit during one period of a cycle?

翻译中…

Input

翻译中…

The first line contains two positive integers $*n*$ and $*m*$ (image), where $*n*$ is the number of rooms, and $*m*$ is the number of doors in the Smile House. Then follows the description of the doors: $*m*$ lines each containing four integers $*i*$, $*j*$, $*c*_{*ij*}$ и $*c*_{*ji*}$ ($1 ≤ *i*, *j* ≤ *n*, *i* ≠ *j*,  - 10^{4} ≤ *c*_{*ij*}, *c*_{*ji*} ≤ 10^{4}$). It is guaranteed that no more than one door connects any two rooms. No door connects the room with itself.

翻译中…

Output

翻译中…

Print the minimum number of rooms that one needs to visit during one traverse of the cycle that can raise mood infinitely. If such cycle does not exist, print number $0$.

翻译中…

Note

翻译中…

Cycle is such a sequence of rooms $*a*_{1}$, $*a*_{2}$, ..., $*a*_{*k*}$, that $*a*_{1}$ is connected with $*a*_{2}$, $*a*_{2}$ is connected with $*a*_{3}$, ..., $*a*_{*k* - 1}$ is connected with $*a*_{*k*}$, $*a*_{*k*}$ is connected with $*a*_{1}$. Some elements of the sequence can coincide, that is, the cycle should not necessarily be simple. The number of rooms in the cycle is considered as $*k*$, the sequence's length. Note that the minimum possible length equals two.

翻译中…

样例

Input #1
4 4
1 2 -10 3
1 3 1 -10
2 4 -10 -1
3 4 0 -3
Output #1
4
InputCopy OutputCopy
题目信息
题号
147B
时间限制
3 s
内存限制
256 MB
标签
二分
图论
矩阵

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。