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

#FW0000061. Người quản lý hộp tiền — vòng di chuyển tối thiểu (USHER — Minimum Round Trip)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Người quản lý hộp tiền — vòng di chuyển tối thiểu (USHER — Minimum Round Trip)

USHER — Minimum Round Trip

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

A money box leaves the manager at vertex 1 and travels along directed transfers with positive costs. Find the minimum total cost of a nonempty closed walk that starts and ends at vertex 1, or -1 if none exists. This is an original PHOJ adaptation inspired by a minimum-cycle technique, NOT a translation of UVa 1233. This task is an original PHOJ adaptation, not the original UVa USHER statement.

Input

First n m; then m directed edges u v cost (1-based).

Output

Print the minimum positive closed-walk cost starting at vertex 1, or -1.

Subtasks

  • Subtask 1 (40 points): 2≤n≤8, 0≤m≤30, 1≤w≤502\le n\le 8,\ 0\le m\le 30,\ 1\le w\le 50. 1 <= u,v <= n and u != v. The graph is directed; parallel edges and disconnected components are allowed.
  • Subtask 2 (60 points): 2≤n≤100, 0≤m≤1000, 1≤w≤1052\le n\le 100,\ 0\le m\le 1000,\ 1\le w\le 10^5. 1 <= u,v <= n and u != v. The graph is directed; parallel edges and disconnected components are allowed.

Examples

Input

3 4
1 2 2
2 3 3
3 1 4
1 3 10

Output

9

Explanation

The 1→2→3→1 closed walk costs 2+3+4=9; the 1→3→1 walk costs 10+4=14, so 9 is optimal.