A delivery man works on a city modelled as a graph with vertices and bidirectional edges with positive distances. The delivery man has to pick and deliver objects in order from 1 to . For each object , he has this information:
the vertex where the object must be picked;
the vertex where the object must be delivered, with ;
the order to deliver the object.
The set is a permutation of . For instance, if , , and , then the objects must be delivered in this order: 2, 3 and 1. In this case, the delivery man has to pick object 1 and afterwards pick object 2. Then, he can decide to pick object 3, or alternatively to deliver object 2. If he decides to deliver object 2, then he has to pick object 3, and so on.
What is the minimum total time to deliver all the objects? The delivery man can choose where to begin and where to end his journey. Assume that the time to pick and deliver objects is neglectable, and that the delivery man can carry as many objects as he wants.
Input consists of several cases. Each case begins with and , followed by triples indicating an edge between and with cost , where . Follow , and the triples in order from 1 to . Assume , , that vertices are numbered starting at 0, , that the graph is connected, that there is at most one edge between each pair of vertices, and .
For each case, print the minimum cost to deliver all the objects.
Input
5 6 0 1 10 0 4 3 1 2 2 1 3 1 1 4 2 2 4 3 2 0 1 2 2 3 1 2 1 1 0 1000000000 3 1 0 3 0 1 2 1 0 1 3 2 1 2 9 1 0 12 2 1 2 1 0 1 2
Output
10 5000000000 42