QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#15138. Candy

Statistics

There are $N$ children in a kindergarten, and teacher lxhgww wants to distribute candies to them, ensuring that every child receives at least one candy.

However, the children are prone to jealousy and often make requests. For example, Xiao Ming might not want Xiao Hong to receive more candies than he does. In total, lxhgww needs to satisfy $K$ such requests while distributing the candies.

Since the supply of candies is limited, lxhgww wants to know the minimum number of candies he needs to prepare to ensure that every child receives at least one candy and all the children's requests are satisfied.

Input

The first line contains two integers $N$ and $K$.

The next $K$ lines describe the relationships that must be satisfied. Each line contains three integers $X, A, B$:

  • If $X=1$, the number of candies received by child $A$ must be equal to the number of candies received by child $B$.
  • If $X=2$, the number of candies received by child $A$ must be less than the number of candies received by child $B$.
  • If $X=3$, the number of candies received by child $A$ must be no less than the number of candies received by child $B$.
  • If $X=4$, the number of candies received by child $A$ must be more than the number of candies received by child $B$.
  • If $X=5$, the number of candies received by child $A$ must be no more than the number of candies received by child $B$.

Output

Output a single line representing the minimum number of candies teacher lxhgww needs to prepare. If it is impossible to satisfy all the children's requests, output $-1$.

Examples

Input 1

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

Output 1

11

Subtasks

For $30\%$ of the data, $N < 100$.

For $100\%$ of the data, $N < 100\,000$, $K \le 100\,000$, $1 \le X \le 5$, $1 \le A, B \le N$.

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.