import
java.io.*;
import
java.util.*;
public
class
GFG {
static
class
Node {
int
a, b, c;
int
dist;
public
Node(
int
a,
int
b,
int
c,
int
dist) {
this
.a = a;
this
.b = b;
this
.c = c;
this
.dist = dist;
}
}
static
boolean
[][][] visited =
new
boolean
[
26
][
26
][
26
];
static
boolean
[][][] restricted =
new
boolean
[
26
][
26
][
26
];
static
int
[] dx = {
1
, -
1
,
0
,
0
,
0
,
0
};
static
int
[] dy = {
0
,
0
,
1
, -
1
,
0
,
0
};
static
int
[] dz = {
0
,
0
,
0
,
0
,
1
, -
1
};
public
static
void
solve(String start, String end,
int
qx,
final
ArrayList<ArrayList<String>> forbidden) {
Arrays.fill(visited[
0
][
0
],
false
);
Arrays.fill(restricted[
0
][
0
],
false
);
for
(ArrayList<String> vec : forbidden) {
String a = vec.get(
0
);
String b = vec.get(
1
);
String c = vec.get(
2
);
for
(
char
x : a.toCharArray())
for
(
char
y : b.toCharArray())
for
(
char
z : c.toCharArray()) {
restricted[x -
'a'
]
[y -
'a'
]
[z -
'a'
]
=
true
;
}
}
int
sa = start.charAt(
0
) -
'a'
;
int
ea = end.charAt(
0
) -
'a'
;
int
sb = start.charAt(
1
) -
'a'
;
int
eb = end.charAt(
1
) -
'a'
;
int
sc = start.charAt(
2
) -
'a'
;
int
ec = end.charAt(
2
) -
'a'
;
if
(restricted[sa][sb][sc]
|| restricted[ea][eb][ec]) {
System.out.println(-
1
);
return
;
}
Queue<Node> q =
new
LinkedList<Node>();
q.add(
new
Node(sa, sb, sc,
0
));
visited[sa][sb][sc] =
true
;
while
(!q.isEmpty()) {
Node x = q.poll();
if
(x.a == (end.charAt(
0
) -
'a'
)
&& x.b == (end.charAt(
1
) -
'a'
)
&& x.c == (end.charAt(
2
) -
'a'
)) {
System.out.println(x.dist);
return
;
}
int
DIST = x.dist;
for
(
int
i =
0
; i <
6
; i++) {
int
A = (x.a + dx[i] +
26
) %
26
;
int
B = (x.b + dy[i] +
26
) %
26
;
int
C = (x.c + dz[i] +
26
) %
26
;
if
(!restricted[A][B][C]
&& !visited[A][B][C]) {
q.add(
new
Node(A, B, C, DIST +
1
));
visited[A][B][C] =
true
;
}
}
}
System.out.println(-
1
);
}
public
static
void
main(String[] args)
{
String X =
"znw"
;
String Y =
"lof"
;
int
N =
4
;
ArrayList<ArrayList<String>> forbidden =
new
ArrayList<ArrayList<String>>(
3
);
forbidden.add(
new
ArrayList<String>(Arrays.asList(
"qlb"
,
"jcm"
,
"mhoq"
)));
forbidden.add(
new
ArrayList<String>(Arrays.asList(
"azn"
,
"piy"
,
"vj"
)));
forbidden.add(
new
ArrayList<String>(Arrays.asList(
"by"
,
"oy"
,
"ubo"
)));
forbidden.add(
new
ArrayList<String>(Arrays.asList(
"jqm"
,
"f"
,
"ej"
)));
solve(X, Y, N, forbidden);
}
}