- Time limit: 1.00 s
- Memory limit: 512 MB
You are playing a game that consists of rooms and tunnels between them. Each room has a letter that you can collect.
Your task is to move from room to room and collect one letter H, two letters I and one letter T during your journey. You can collect the letters in any order. What is the minimum length of such a path?
Input
The first input line has two integers and : the number of rooms and tunnels. The rooms are numbered .
The second line has characters : the letter (A–Z) in each room.
Finally, there are lines that describe the tunnels. Each line has two integers and : there is a tunnel between rooms and .
Output
Print one integer: the minimum length of a valid path from room to room . If there is no such path, print "IMPOSSIBLE".
Constraints
Example 1
Input:
5 6 ITHXI 1 2 1 3 1 4 3 4 3 5 4 5
Output:
4
Explanation: An optimal path is .
Example 2
Input:
3 2 HIT 1 2 2 3
Output:
IMPOSSIBLE
Explanation: Only one letter I is available, so you can't win the game.