Time Machine

Compute the fastest times from city 1 over bus routes with possibly negative durations, or print -1 when a reachable negative cycle exists.

Medium4Shortest pathGraphNo attempts yetTime limit1sMemory limit256 MB

Problem

There are NN cities and MM bus routes, each running from one city to another. A route is written as three integers AA, BB, CC, where AA is the departure city, BB is the arrival city, and CC is the time the ride takes.

The time CC is not always positive. When C=0C = 0 the ride is instant, and when C<0C < 0 the bus is a time machine that sends you back in time.

Write a program that finds the fastest time to travel from city 1 to every other city.

Input

The first line has the number of cities NN (1N5001 \le N \le 500) and the number of bus routes MM (1M60001 \le M \le 6000).

Each of the next MM lines has one route AA, BB, CC (1A,BN1 \le A, B \le N, 10000C10000-10000 \le C \le 10000). Several routes may connect the same pair of cities, and a route with A=BA = B may appear.

Output

If a trip that starts at city 1 can push the time back without bound, print -1 on the first line.

Otherwise print N1N - 1 lines holding the fastest time from city 1 to city 2, city 3, ..., city NN, one time per line in that order. If a city has no route from city 1, print -1 on its line. When N=1N = 1, print nothing.