HIKE - Hike on a Graph

"Hike on a Graph" is a game that is played on a board on which an undirected graph is drawn. The graph is complete and has all loops, i.e. for any two locations there is exactly one arrow between them. The arrows are coloured. There are three players, and each of them has a piece. At the beginning of the game, the three pieces are in fixed locations on the graph. In turn, the players may do a move. A move consists of moving one's own piece along an arrow to a new location on the board. The following constraint is imposed on this: the piece may only be moved along arrows of the same colour as the arrow between the two opponents' pieces.

In the sixties ("make love not war") a one-person variant of the game emerged. In this variant one person moves all the three pieces, not necessarily one after the other, but of course only one at a time. Goal of this game is to get all pieces onto the same location, using as few moves as possible. Find out the smallest number of moves that is necessary to get all three pieces onto the same location, for a given board layout and starting positions.

Input

The input file contains several test cases. Each test case starts with the number n. Input is terminated by n=0. Otherwise, 1<=n<=50. Then follow three integers p1, p2, p3 with 1<=pi<=n denoting the starting locations of the game pieces. The colours of the arrows are given next as a n×n matrix m of whitespace-separated lower-case letters. The element mij denotes the colour of the arrow between the locations i and j. Since the graph is undirected, you can assume the matrix to be symmetrical.

Output

For each test case output on a single line the minimum number of moves required to get all three pieces onto the same location, or the word "impossible" if that is not possible for the given board and starting locations.

Example

Input:
3 1 2 3
r b r
b b b
r b r
2 1 2 2
y g
g y
0

Output:
2
impossible

Added by:Adrian Kuegel
Date:2005-07-10
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:University of Ulm Local Contest 2000

hide comments
2022-01-10 03:56:13
What is the maximum number of test cases? It's hard to be sure whether my program is fast enough without it.
2019-01-17 03:47:17
This is confusing: "the piece may only be moved along arrows of the same colour as the arrow between the two opponents' pieces..."
Test case that is helpful:
5 1 3 5
r d r c b
d c b r c
r b r r b
c r r b b
b c b b c
0

output:
4
2017-04-16 11:13:51
we nide mor time
2017-04-04 21:36:25
excellently framed problem,,,
2017-03-01 19:22:02
Really good and easy problem! :)
2016-04-18 19:06:40
Excellent problem! Really enjoyed solving this.

@Rohan, I've just submitted a solution java. The time limit is fine.
2015-06-04 12:15:13 Vaibhav Gosain
there is some problem in the input files. It contains unnecessary whitespace.
2014-09-17 19:20:11 Deepak Gupta
Remember, different edges from a single source can have same color
2013-11-20 13:35:48 Julian Waldby
How can I tell what color the pieces are?
2013-05-13 23:14:54 Rohan Garg
The time limit for Java must be increased ! Same solution in C++ passes but gets TLE in Java
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.