You are playing the following game. There are n points on a plane. They are the vertices of a regular n-polygon. Points are labeled with integer numbers from 1 to n. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. There are three stones positioned on three distinct vertices. All stones are the same. With one move you can move the stone to another free vertex along some diagonal. The color of this diagonal must be the same as the color of the diagonal, connecting another two stones. 

Your goal is to move stones in such way that the only vertices occupied by stones are 1, 2 and 3. You must achieve such position using minimal number of moves. Write a program which plays this game in an optimal way.


-----Input-----

In the first line there is one integer n (3 ≤ n ≤ 70) — the number of points. In the second line there are three space-separated integer from 1 to n — numbers of vertices, where stones are initially located.

Each of the following n lines contains n symbols — the matrix denoting the colors of the diagonals. Colors are denoted by lowercase English letters. The symbol j of line i denotes the color of diagonal between points i and j. Matrix is symmetric, so j-th symbol of i-th line is equal to i-th symbol of j-th line. Main diagonal is filled with '*' symbols because there is no diagonal, connecting point to itself.


-----Output-----

If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the point to which move the stone. If there are several optimal solutions, print any of them.


-----Examples-----
Input
4
2 3 4
*aba
a*ab
ba*b
abb*

Output
1
4 1

Input
4
2 3 4
*abc
a*ab
ba*b
cbb*

Output
-1



-----Note-----

In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3.

Source: codeforces | Difficulty: HARD
Tags: Dynamic programming

Graph algorithms

Implementation

Shortest paths


Solutions:
from collections import deque
__author__ = 'asmn'
n = int(input())
end = tuple(sorted([int(x) - 1 for x in input().split()]))
st = (0, 1, 2)
mat = [input() for i in range(n)]
v = set([st])
path = {}
dist = {st: 0}
queue = deque([st])
while end not in v and len(queue) > 0:
	p = queue.popleft()
	for x in range(-2, 1):
		(p1, p2, p3) = (p[x], p[x + 1], p[x + 2])
		for i in range(n):
			if i not in (p1, p2, p3) and mat[i][p3] == mat[p1][p2]:
				np = tuple(sorted((p1, p2, i)))
				if np not in v:
					v.add(np)
					queue.append(np)
					path[np] = p
					dist[np] = dist[p] + 1

def pathinfo(fr, to):
	return str((set(fr) - set(to)).pop() + 1) + ' ' + str((set(to) - set(fr)).pop() + 1)
if end not in dist:
	print(-1)
	return
print(dist[end])
while end in path:
	print(pathinfo(end, path[end]))
	end = path[end]