Road Renovation

No attempts yetTime limit1sMemory limit128 MB

Problem

Most of the roads in Byteland are in terrible condition. Responding to numerous requests from his subjects, the King of Byteland has decided to renovate some of the roads. Byteland has nn cities numbered from 11 to nn. Some ordered pairs of cities are joined by one-way roads. The chief builder picked mm roads that he considers worth renovating and estimated a repair cost for each one.

The King wants every citizen to feel the improvement firsthand. He decided that the residents of a city are satisfied only if, using renovated roads, one can both enter that city and leave that city. Plan the renovations so that their total cost is as small as possible.

In other words, choose a subset of the given roads so that every city has at least one chosen road leaving it and at least one chosen road entering it, and the total cost of the chosen roads is minimized.

Input

The first line contains two integers nn and mm (2n3002 \le n \le 300, 1mn21 \le m \le n^2): the number of cities and the number of one-way roads available for renovation. Each of the next mm lines contains three integers xx, yy, and kk (1x,yn1 \le x, y \le n, 0k1050 \le k \le 10^5), meaning that renovating the road from city xx to city yy costs kk. Each ordered pair (x,y)(x, y) appears at most once in the input. A road may start and end at the same city.

Output

Print a single integer: the minimum possible total cost of a renovation plan that meets the King's requirement. If no such plan exists, print NIE instead.