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

#BF0000015. Đường đi ngắn nhất toàn đồ thị (The Shortest Path)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường đi ngắn nhất toàn đồ thị (The Shortest Path)

The Shortest Path

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a directed weighted graph, consider all ordered pairs of distinct vertices u≠vu\ne v for which a walk from uu to vv exists. Find the minimum, over these pairs, of the shortest-walk cost. Walks may repeat vertices. If a negative cycle makes the minimum unbounded below, report negative infinity. Empty walks from a vertex to itself must not be counted.

Input

The first line contains n,mn,m. Then follow mm lines u,v,cu,v,c for edges u→vu\to v with cost cc. Self-loops are forbidden, but parallel edges are allowed.

Output

Print -inf when the answer is unbounded below; otherwise print the minimum cost between distinct vertices.

Subtasks

  • Subtask 1 (20 points): 2≤n≤152\le n\le15; 1≤m≤701\le m\le70; −100≤w≤100-100\le w\le100; u≠vu\ne v on each edge; parallel edges allowed.

  • Subtask 2 (30 points): 2≤n≤1002\le n\le100; 1≤m≤10001\le m\le1000; −10000≤w≤10000-10000\le w\le10000; u≠vu\ne v on each edge; parallel edges allowed.

  • Subtask 3 (50 points): 2≤n≤20002\le n\le2000; 1≤m≤50001\le m\le5000; −1000000≤w≤1000000-1000000\le w\le1000000; u≠vu\ne v on each edge; parallel edges allowed.

Examples

Input

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

Output

-6

Explanation

The walk 2→3→42\to3\to4 costs −3+(−3)=−6-3+(-3)=-6, the minimum over all ordered pairs of distinct vertices.