The Longest Travel Route

Given a directed weighted graph with at most 18 cities, find the maximum total length of a simple path from city 0 to city n-1.

Medium6Dynamic programmingBit manipulationGraphDFSInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Plenty of well known algorithms find the shortest route from one place to another. The GPS units in cars and phones tell you the fastest way to reach your destination. On vacation, though, Troy likes to travel slowly. He wants to see many new and interesting places on the way, so he takes the longest route to his destination.

A route is a sequence of distinct cities c1,c2,,ckc_1, c_2, \dots, c_k such that for every 1i<k1 \le i < k there is a road from cic_i to ci+1c_{i+1}. Troy never visits the same city twice.

Find the length of the longest route.

Input

The first line contains the number of cities nn and the number of roads connecting them mm (2n182 \le n \le 18, 1mn2n1 \le m \le n^2 - n). There is at most one road from any given city to any other given city. Cities are numbered from 00 to n1n-1, where 00 is the city Troy starts in and n1n-1 is his destination.

Each of the next mm lines contains three integers ss, dd, ll, meaning that there is a road of length ll km from city ss to city dd (0sn10 \le s \le n-1, 0dn10 \le d \le n-1, sds \ne d, 1l100001 \le l \le 10000). Every road is one way, so it can be taken from ss to dd only, not in the opposite direction.

There is always at least one route from city 00 to city n1n-1.

Output

Print a single integer, the length of the longest route that starts in city 00, ends in city n1n-1, and visits no city twice. The length of a route is the sum of the lengths of the roads it takes.