A wizard is in a labyrinth where there are rooms and doors connecting some pairs of rooms in both directions,in such a way that there is always a sequence of doors one can traverse to go from a room to any other room.
Additionally,there are locks and keys of different colours(one of each)in some of the doors and rooms of the maze,respectively;each door has at most one lock,and there is at most one key placed in each room.It should be an easy matter for the wizard to bypass the lock system,were it not for the fact that he forgot his spell book,without which his wizardry is utterly useless.The wizard is currently in room ,and he wants to get his spell book,located in room ,without taking too long.At every step he may go to an adjacent room through one of the doors.Of course,if the door is locked,he needs to be carrying the key of the same colour as the lock(unless,of course,that door has already been unlocked).The wizard can carry only one key at a time and after picking up a key it is not possible for him to drop it somewhere in the maze in order to take it again afterwards.Once a door has been unlocked,the key is thrown away since it is no longer any use.
Given the maze and the positions of the keys and locks,determine how to reach from ,if possible.Any path whose length does not exceed is acceptable.
The first line of each case contains four integers:the number of rooms in the maze ,the number of locks ,and rooms and numbered .
Then comes a(possibly empty)line with integers indicating the location of each of the keys,in order of increasing colour.
The next lines describe the maze:each contains three integers ,meaning that there is a door between rooms and which can be unlocked with the key of colour ,if ;a value of for indicates that no lock is needed.