QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100

#8760. Inequality

Statistiques

Given $n, m$, and $m$ conditions of the form $a_{x_i} \ge a_{y_i} + a_{z_i}$ ($1 \le i \le m$), determine whether there exists a set of positive integers $(a_1, a_2, \dots, a_n)$ that satisfies all conditions such that $a_1 + a_2 + \dots + a_n \le 10^9$. If such a set exists, output the minimum value of $a_1 + a_2 + \dots + a_n$; otherwise, output $-1$.

Input

The first line contains two integers $n$ and $m$ ($1 \le n, m \le 2 \times 10^5$).

Each of the following $m$ lines contains three integers $x_i, y_i, z_i$, representing a constraint $a_{x_i} \ge a_{y_i} + a_{z_i}$ ($1 \le x_i, y_i, z_i \le n$).

Output

Output a single integer representing the answer.

Examples

Input 1

5 2
1 2 3
3 4 5

Output 1

8

Note

The solution with the minimum sum is $(3, 1, 2, 1, 1)$, which gives a sum of $8$.

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.