Good Code
View as PDFIt's time for the members of The Team to do what they do best - coding!
Faster than you can say "Dijkstra", they've already produced an elegant
piece of work. The program has  
 lines of code
(conveniently numbered 
), and just one integer variable, 
,
which starts with a value of 0. The program starts executing at line 1,
and every line 
 contains one of the following:
c++;— The value ofis incremented by 1. Then, if
, the program terminates - otherwise, the program moves to line
.
x:— This line contains the label, where
is an integer such that
. No value of
will appear as a label more than once in the program. If
, the program terminates - otherwise, the program moves to line
.
goto x;— The program jumps to the single line that contains the label, where
is an integer such that
. It is guaranteed that, for every such line, the corresponding label will exist in the program.
Now, even though this program is glorious, its creators are wondering if
it's quite correct. In particular, they know that  should ideally
reach a value of 
 
, but they're not sure when.
If the program terminates with 
, then certainly there's an
issue, and the program should get a WA (Wrong Answer). If the program
will never terminate, and 
 will never reach a value of 
, then
that's also no good, and can be considered a TLE (Time Limit Exceeded).
In all other cases, however, The Team would like to know exactly on
which line 
 will first attain a value of 
. Naturally, having
written the program, they instantly realized this already. But can you?
Input Specification
First line: 2 integers,  and 
Next  lines: The 
-th line of the program (as described above), for
Output Specification
Either 1 integer, the number of the line on which  will first reach a
value of 
, or the string 
WA if the program terminates with ,
or the string 
TLE if the program runs forever with .
Sample Input
12 4
c++;
goto 6;
18:
c++;
c++;
goto 2;
goto 6;
6:
goto 18;
2:
c++;
c++;
Sample Output
11
Explanation of Sample
The program will run through the following lines, and corresponding
values of :
- Line 1 (
c++;), - Line 2 (
goto 6;), - Line 8 (
6:), - Line 9 (
goto 18;), - Line 3 (
18:), - Line 4 (
c++;), - Line 5 (
c++;), - Line 6 (
goto 2;), - Line 10 (
2:), - Line 11 (
c++;), 
As can be seen,  first achieves a value of 
 on line 11.
Comments