eolymp
bolt
Try our new interface for solving problems
Problems

Harry the Hamster

Harry the Hamster

Harry the Hamster lives in a giant hamster cage. Inside the cage there is a set of n plastic balls connected by unidirectional hamster tubes of varying lengths. Harry is currently in ball s and his bed is in ball t.

Being a simple hamster, Harry’s brain halves are not so great at communicating with each other and have a mind of their own. Harry’s left brain half, usually being active when Harry is in the hamster wheel, likes running for as long as possible. Harry’s right brain half, rarely active at all, would like to go to sleep as soon as possible. Together, Harry’s brain halves will be navigating Harry through the maze of tubes, in each ball deciding which of the outgoing tubes to follow.

Harry’s brain halves get really tired after making a decision and then need to rest a bit, so they cannot make two decisions in a row. Thus, they make decisions on which tube to take in alternating turns, with the left brain half going first. So starting in ball s, Harry’s left brain half will decide on a tube to follow, ending in some ball u, where Harry’s left brain half will rest and Harry’s right brain half will pick an outgoing tube, et cetera.

Counterintuitively, the brain halves are familiar with the entire hamster cage and can plan arbitrarily far ahead. Assuming both brain halves make optimal decisions, how long will it take for Harry to reach his bed? It is guaranteed that each ball has at least one outgoing tube, except the ball containing Harry’s bed which has none (there Harry will rest easily). There are no tubes connecting a ball to itself, but there may be multiple tubes going from one ball to another.

Input

First line contains four space-separated integers: the number of plastic balls n (1n105), the number of tubes m (0m2 * 105), and the locations of Harry and his bed s, t (0s, t < n).

Then m lines follow, each containing three space-separated integers describing a single tube: the ball in which the tube starts ai (0ai < n), in which it ends bi (0bi < n) and the time it takes to traverse wi (1wi104). Note that each tube can only be traversed in one direction.

Output

Print the time it takes for Harry to reach his bed, or the string infinity if Harry is doomed to roam the tubes forever.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4 5 0 3
0 1 1
1 2 2
2 0 4
2 3 1
2 3 3
Output example #1
11
Input example #2
5 5 0 4
0 1 1
1 2 1
2 3 1
3 0 1
2 4 1
Output example #2
infinity
Input example #3
2 1 0 1
0 1 2
Output example #3
2
Input example #4
3 3 1 2
0 1 1
1 0 1
1 2 1
Output example #4
infinity
Input example #5
3 2 0 1
0 2 3
2 0 3
Output example #5
infinity
Source 2018 Benelux Algorithm Programming Contest (BAPC), Problem H