Problem list
Log in
#boj00001 · Online Judge > BOJ

Spy

Platinum III 1.0 s 256 MB

SPARCS Company, whose president is Seokju, has decided to launch $N$ new products! The company's $K$ beta testers conferred with each other and assigned every new product an integer rating between 0 and 100, inclusive. Curious about the beta-test results, Jihoon — a spy working for the rival GoN Company — asked each of the $K$ beta testers one question and obtained one piece of information about the ratings from each. The information comes in three kinds:

  • 1 a b c : (rating of product $a$) − (rating of product $b$) $\ge c$.
  • 2 a b c : (rating of product $a$) − (rating of product $b$) $\le c$.
  • 3 a b c : (rating of product $a$) − (rating of product $b$) $= c$.

Using this data Jihoon tried to figure out which new product was rated best and which was rated worst, but that turned out to be impossible. Settling for less, let us compute the minimum possible value of (highest rating) − (lowest rating).

Input

The first line contains $N$ and $K$ ($2 \le N \le 1000$, $1 \le K \le 3000$). Each of the next $K$ lines contains one piece of information Jihoon obtained, in the format described above ($1 \le a \le N$, $1 \le b \le N$, $|c| \le 100$).

Output

Print the minimum possible value of (highest rating) − (lowest rating) over all rating assignments consistent with every piece of information Jihoon obtained. If no consistent assignment exists, print -1.

Example 1

Input
3 1
1 2 3 3
Output
3
Source
Online Judge > BOJ
Appears in
Not part of a ladder, contest or set yet.
Accepted 1 Submissions 1 Acceptance 100% Solvers 1

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code