PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BF0000019. Điểm số cao nhất (High Score)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Điểm số cao nhất (High Score)

High Score

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

A game has nn rooms and mm one-way tunnels. Begin at room 11 with score 00. Each traverse of a tunnel from aa to bb changes the score by signed integer xx. Tunnels can be reused. Find the maximum score attainable on reaching room nn, guaranteed reachable. Print -1 if the score is unbounded above.

Input

The first line has n,mn,m; each of the next mm lines contains a,b,xa,b,x for a one-way tunnel.

Output

Print -1 if the score is unbounded; otherwise the maximum score.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le15; 1≤m≤501\le m\le50; −100≤w≤100-100\le w\le100; target nn reachable from 11.

  • Subtask 2 (30 points): 1≤n≤1201\le n\le120; 1≤m≤7001\le m\le700; −1000000≤w≤1000000-1000000\le w\le1000000; target nn reachable from 11.

  • Subtask 3 (50 points): 1≤n≤25001\le n\le2500; 1≤m≤50001\le m\le5000; −1000000000≤w≤1000000000-1000000000\le w\le1000000000; target nn reachable from 11.

Examples

Input

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

Output

5

Explanation

The route 1→3→41\to3\to4 scores −2+7=5-2+7=5, the maximum.