Problem: Pak Chanek is playing one of his favourite board games. In the game, there is a directed graph with vertices and edges. In the graph, edge connects two different vertices and with a length of . By using the -th edge, something can move from to , but not from to .
To play this game, initially Pak Chanek must place both of his hands onto two different vertices. In one move, he can move one of his hands to another vertex using an edge. To move a hand from vertex to vertex , Pak Chanek needs a time of seconds. Note that Pak Chanek can only move one hand at a time. This game ends when both of Pak Chanek's hands are on the same vertex.
Pak Chanek has several questions. For each satisfying , you need to find the minimum time in seconds needed for Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex and vertex , or report if it is impossible.
Input Format: The first line contains two integers and (, ) — the number of vertices and edges in the graph.
The -th of the next lines contains three integers , , and (, , ) — a directed edge that connects two different vertices and with a length of . There is no pair of different edges and such that and .
Output Format: Output a line containing integers. The -th integer represents the minimum time in seconds needed by Pak Chanek to end the game if initially Pak Chanek's left hand and right hand are placed on vertex and vertex , or if it is impossible.
Note: If initially Pak Chanek's left hand is on vertex and his right hand is on vertex , Pak Chanek can do the following moves:
- Move his right hand to vertex in second.
- Move his left hand to vertex in seconds.
- Move his left hand to vertex in second.
In total it needs seconds. It can be proven that there is no other way that is faster.