Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2 × 2 grid and three tiles labeled 'A', 'B', and 'C'. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below: $\rightarrow$ 

In order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.


-----Input-----

The first two lines of the input consist of a 2 × 2 grid describing the initial configuration of Bessie's puzzle. The next two lines contain a 2 × 2 grid describing the initial configuration of Elsie's puzzle. The positions of the tiles are labeled 'A', 'B', and 'C', while the empty cell is labeled 'X'. It's guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.


-----Output-----

Output "YES"(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print "NO" (without quotes).


-----Examples-----
Input
AB
XC
XB
AC

Output
YES

Input
AB
XC
AC
BX

Output
NO



-----Note-----

The solution to the first sample is described by the image. All Bessie needs to do is slide her 'A' tile down.

In the second sample, the two puzzles can never be in the same configuration. Perhaps Bessie and Elsie are not meant to be friends after all...

Difficulty: EASY
Tags: Complete search

Implementation

Constructive algorithms
Skill types: Complete search

Starter code:

Solutions:
a1 = input()
a2 = input()
b1 = input()
b2 = input()
first = list(a1 + a2[::-1])
second = list(b1 + b2[::-1])
first.remove('X')
second.remove('X')
ans = 'NO'
for i in range(3):
	if first == second:
		ans = 'YES'
		break
	second = second[1:] + [second[0]]
print(ans)


def find_order(A, B):
	d = dict()
	i = 0
	if A[0] != 'X':
		d[i] = A[0]
		i += 1
	if A[1] != 'X':
		d[i] = A[1]
		i += 1
	if B[1] != 'X':
		d[i] = B[1]
		i += 1
	if B[0] != 'X':
		d[i] = B[0]
		i += 1
	return d
a = input()
b = input()
c = input()
d = input()
m = find_order(a, b)
n = find_order(c, d)
m[3] = m[0]
m[4] = m[1]
if m[0] == n[0]:
	if m[1] == n[1] and m[2] == n[2]:
		print('YES')
	else:
		print('NO')
elif m[1] == n[0]:
	if m[2] == n[1] and m[3] == n[2]:
		print('YES')
	else:
		print('NO')
elif m[2] == n[0]:
	if m[3] == n[1] and m[4] == n[2]:
		print('YES')
	else:
		print('NO')


import copy

def g(s):
	ans = set()
	q = [s]
	while q:
		t = q.pop(0)
		for i in range(2):
			for j in range(2):
				if t[i][j] == 'X':
					xx = copy.deepcopy(t)
					(xx[i][j], xx[i][1 - j]) = (xx[i][1 - j], xx[i][j])
					k = ''.join(map(lambda r: ''.join(r), xx))
					if k not in ans:
						ans.add(k)
						q.append(xx)
					xx = copy.deepcopy(t)
					(xx[i][j], xx[1 - i][j]) = (xx[1 - i][j], xx[i][j])
					k = ''.join(map(lambda r: ''.join(r), xx))
					if k not in ans:
						ans.add(k)
						q.append(xx)
	return ans
t = []
t.append(list(input()))
t.append(list(input()))
r = []
r.append(list(input()))
r.append(list(input()))
print('YES' if g(r) & g(t) else 'NO')


import re
(s1, s2, s3, s4) = [input().rstrip() for _ in range(4)]
(t1, t2) = (s1 + s2[::-1], s3 + s4[::-1])
t1 = re.sub('X', '', t1)
t2 = re.sub('X', '', t2)
if len(t1) == len(t2) and (t1 + t1).find(t2) != -1:
	print('YES')
else:
	print('NO')


3

def readln():
	return list(map(int, input().split()))

def main():
	inp = [input() for _ in range(4)]
	a = inp[0] + inp[1][1] + inp[1][0]
	a = a.replace('X', '')
	b = inp[2] + inp[3][1] + inp[3][0]
	b = b.replace('X', '')
	print('YES' if a == b or a == b[1:] + b[0] or a == b[2] + b[:2] else 'NO')

def __starting_point():
	main()
__starting_point()


def read(s, t):
	if s[0] == 'X':
		return s[1] + t[::-1]
	if s[1] == 'X':
		return t[::-1] + s[0]
	if t[0] == 'X':
		return t[1] + s
	if t[1] == 'X':
		return s + t[0]
s1 = ''.join(input().split())
s2 = ''.join(input().split())
t1 = ''.join(input().split())
t2 = ''.join(input().split())
s = read(s1, s2)
t = read(t1, t2)
for i in range(3):
	for j in range(3):
		if s[i:] + s[:i] == t[j:] + t[:j]:
			print('YES')
			exit(0)
print('NO')


def main():
	(a1, a2, b1, b2) = (input(), input(), input(), input())
	combs1 = set(['AB', 'BC', 'CA'])
	combs2 = set(['BA', 'CB', 'AC'])
	type1 = a1 in combs1 or a2 in combs2
	type2 = b1 in combs1 or b2 in combs2
	print('YES' if type1 == type2 else 'NO')
main()


A = (input() + input()[::-1]).replace('X', '')
B = (input() + input()[::-1]).replace('X', '')
while A[0] != 'A':
	A = A[1:] + A[0]
while B[0] != 'A':
	B = B[1:] + B[0]
print('YES' if A == B else 'NO')


a = input().strip() + input().strip()[::-1]
b = input().strip() + input().strip()[::-1]
a = a.replace('X', '')
b = b.replace('X', '')
if a == b or a[1:] + a[:1] == b or a[2:] + a[:2] == b:
	print('YES')
else:
	print('NO')


a = [list(input()) for _ in range(2)]
b = [list(input()) for _ in range(2)]
a = [a[0][0], a[0][1], a[1][1], a[1][0]]
b = [b[0][0], b[0][1], b[1][1], b[1][0]]
a.remove('X')
b.remove('X')
x = a.index('A')
y = b.index('A')
ret = True
for i in range(3):
	if a[(x + i) % 3] != b[(y + i) % 3]:
		ret = False
if ret:
	print('YES')
else:
	print('NO')


ch = [str(input()) for _ in range(4)]
L = [ch[0][0], ch[0][1], ch[1][1], ch[1][0]]
R = [ch[2][0], ch[2][1], ch[3][1], ch[3][0]]
del L[L.index('X')]
del R[R.index('X')]
i = 0
while i < 4 and L != R:
	i += 1
	L = L[1:] + [L[0]]
if L == R:
	print('YES')
else:
	print('NO')


def main():
	grid1 = []
	grid1.append(list(input()))
	grid1.append(list(input()))
	grid2 = []
	grid2.append(list(input()))
	grid2.append(list(input()))
	print(solver(grid1, grid2))

def solver(grid1, grid2):
	if grid1 == grid2:
		return 'YES'
	else:
		for i in range(11):
			rotateClockwise(grid1)
			if grid1 == grid2:
				return 'YES'
		return 'NO'

def rotateClockwise(grid):
	if grid[0][0] == 'X':
		(grid[0][0], grid[1][0]) = (grid[1][0], grid[0][0])
	elif grid[0][1] == 'X':
		(grid[0][0], grid[0][1]) = (grid[0][1], grid[0][0])
	elif grid[1][0] == 'X':
		(grid[1][0], grid[1][1]) = (grid[1][1], grid[1][0])
	else:
		(grid[0][1], grid[1][1]) = (grid[1][1], grid[0][1])
main()


a = input() + input()
a = a.replace('', ' ').split()
b = input() + input()
b = b.replace('', ' ').split()
for _ in ' ' * 16:
	k = a.index('X')
	if k == 2:
		(a[2], a[3]) = (a[3], a[2])
	elif k == 0:
		(a[0], a[2]) = (a[2], a[0])
	elif k == 3:
		(a[3], a[1]) = (a[1], a[3])
	else:
		(a[1], a[0]) = (a[0], a[1])
	if a == b:
		print('YES')
		break
else:
	print('NO')


import math
from math import floor
a11 = input()
a21 = input()
b11 = input()
b21 = input()
sList = []
sList.append(a11[0])
sList.append(a11[1])
sList.append(a21[1])
sList.append(a21[0])
sList.remove('X')
sList.append(sList[0])
sList.append(sList[1])
pList = []
pList.append(b11[0])
pList.append(b11[1])
pList.append(b21[1])
pList.append(b21[0])
pList.remove('X')
s1 = s2 = ''
for i in sList:
	s1 += i
for i in pList:
	s2 += i
if s1.find(s2) == -1:
	print('NO')
else:
	print('YES')


def roll(x):
	x = x.replace('X', '')
	while x[0] != 'A':
		x = x[-1] + x[:2]
	return x
(a, b) = (input() + input()[::-1], input() + input()[::-1])
print(['NO', 'YES'][roll(a) == roll(b)])


a_row1 = input()
a_row2 = input()
b_row1 = input()
b_row2 = input()
a_sequence = a_row1 + a_row2[1] + a_row2[0]
b_sequence = b_row1 + b_row2[1] + b_row2[0]
a_sequence = a_sequence.replace('X', '')
b_sequence = b_sequence.replace('X', '')
same = False
for _ in range(3):
	if a_sequence == b_sequence:
		same = True
		break
	else:
		a_sequence = a_sequence[2] + a_sequence[0:2]
if same:
	print('YES')
else:
	print('NO')


def find(let, data):
	for i in range(2):
		for j in range(2):
			if data[i][j] == let:
				return (i, j)
data1 = [input(), input()]
data2 = [input(), input()]
a1 = data1[0][0] + data1[0][1] + data1[1][0] + data1[1][1]
b1 = data2[0][0] + data2[0][1] + data2[1][0] + data2[1][1]
ind = a1.find('A')
if ind == 0:
	sos = a1[1]
elif ind == 1:
	sos = a1[3]
elif ind == 2:
	sos = a1[0]
else:
	sos = a1[2]
ind = b1.find('A')
if ind == 0:
	sos1 = b1[1]
elif ind == 1:
	sos1 = b1[3]
elif ind == 2:
	sos1 = b1[0]
else:
	sos1 = b1[2]
if sos == 'X':
	ind = a1.find('X')
	if ind == 0:
		sos = a1[1]
	elif ind == 1:
		sos = a1[3]
	elif ind == 2:
		sos = a1[0]
	else:
		sos = a1[2]
if sos1 == 'X':
	ind = b1.find('X')
	if ind == 0:
		sos1 = b1[1]
	elif ind == 1:
		sos1 = b1[3]
	elif ind == 2:
		sos1 = b1[0]
	else:
		sos1 = b1[2]
if sos == sos1:
	print('YES')
else:
	print('NO')


puzzle1 = ''
puzzle2 = ''
row = input()
if row[0] != 'X':
	puzzle1 += row[0]
if row[1] != 'X':
	puzzle1 += row[1]
row = input()
if row[1] != 'X':
	puzzle1 += row[1]
if row[0] != 'X':
	puzzle1 += row[0]
row = input()
if row[0] != 'X':
	puzzle2 += row[0]
if row[1] != 'X':
	puzzle2 += row[1]
row = input()
if row[1] != 'X':
	puzzle2 += row[1]
if row[0] != 'X':
	puzzle2 += row[0]
var = [puzzle2, puzzle2[2] + puzzle2[0] + puzzle2[1], puzzle2[1] + puzzle2[2] + puzzle2[0]]
if puzzle1 in var:
	print('YES')
else:
	print('NO')


a = input().strip()
a = a + ''.join(list(reversed(input().strip())))
b = input().strip()
b = b + ''.join(list(reversed(input().strip())))
a = a.replace('X', '')
b = b.replace('X', '')
f = False
for i in range(4):
	if a == b:
		f = True
	b = b[1:] + b[:1]
if f:
	print('YES')
else:
	print('NO')


a = ''.join(input())
a = a.join(input())
a = list(a)
a.remove('X')
a = ''.join(map(str, a))
b = ''.join(input())
b = b.join(input())
b = list(b)
b.remove('X')
b = ''.join(map(str, b))
c = b + b + b + b + b + b
if a in c:
	print('YES')
else:
	print('NO')


b1 = input()
b2 = input()
e1 = input()
e2 = input()
b = (b1 + b2[::-1]).split('X')
e = (e1 + e2[::-1]).split('X')
b = b[1] + b[0]
e = e[0] + e[1]
if e == b:
	print('YES')
elif e[2] + e[0] + e[1] == b:
	print('YES')
elif e[1] + e[2] + e[0] == b:
	print('YES')
else:
	print('NO')


a1 = input()
a2 = input()
b1 = input()
b2 = input()
bel = [a1, a2]
el = [b1, b2]
checker = False
for i in range(100):
	if not (a1 == b1 and a2 == b2):
		if 'X' in a1:
			if a1.find('X') == 0:
				a1 = a2[0] + a1[1]
				a2 = 'X' + a2[1]
			else:
				a1 = 'X' + a1[0]
		elif a2.find('X') == 0:
			a2 = a2[1] + 'X'
		else:
			a2 = a2[0] + a1[1]
			a1 = a1[0] + 'X'
	else:
		checker = True
		break
if checker == True:
	print('YES')
else:
	print('NO')


per1 = input()
per2 = input()
per3 = input()
per4 = input()
s = []
s.append(per1[0])
s.append(per1[1])
s.append(per2[1])
s.append(per2[0])
for i in range(len(s)):
	if s[i] == 'X':
		s.pop(i)
		break
d = []
d.append(per3[0])
d.append(per3[1])
d.append(per4[1])
d.append(per4[0])
for i in range(len(d)):
	if d[i] == 'X':
		d.pop(i)
		break
per2 = False
for i in range(3):
	if s != d:
		t = d.pop(0)
		d.append(t)
	else:
		per2 = True
		break
if per2:
	print('YES')
else:
	print('NO')


import sys
pa = input() + input()
pb = input() + input()
mark = set()
qt = [pb]
while qt:
	s = qt.pop()
	if s == pa:
		print('YES')
		sys.exit(0)
	if s in mark:
		continue
	mark.add(s)
	tb = [list(s[:2]), list(s[2:])]
	x = y = None
	for i in range(0, 2):
		for j in range(0, 2):
			if tb[i][j] == 'X':
				(x, y) = (i, j)
				break
	if x - 1 >= 0:
		ntb = [tb[0][:], tb[1][:]]
		(ntb[x - 1][y], ntb[x][y]) = (ntb[x][y], ntb[x - 1][y])
		qt.append(''.join(ntb[0] + ntb[1]))
	if x + 1 < 2:
		ntb = [tb[0][:], tb[1][:]]
		(ntb[x + 1][y], ntb[x][y]) = (ntb[x][y], ntb[x + 1][y])
		qt.append(''.join(ntb[0] + ntb[1]))
	if y - 1 >= 0:
		ntb = [tb[0][:], tb[1][:]]
		(ntb[x][y - 1], ntb[x][y]) = (ntb[x][y], ntb[x][y - 1])
		qt.append(''.join(ntb[0] + ntb[1]))
	if y + 1 < 2:
		ntb = [tb[0][:], tb[1][:]]
		(ntb[x][y + 1], ntb[x][y]) = (ntb[x][y], ntb[x][y + 1])
		qt.append(''.join(ntb[0] + ntb[1]))
print('NO')


a = input() + input()[::-1]
b = input() + input()[::-1]
a = a.replace('X', '')
b = b.replace('X', '')
if (a + a).find(b) + 1:
	print('YES')
else:
	print('NO')


d1 = list(input() + input()[::-1])
d2 = list(input() + input()[::-1])
j = d1.index('X')
k = d2.index('X')
L = []
for i in range(12):
	(d1[j % 4], d1[(j + 1) % 4]) = (d1[(j + 1) % 4], d1[j % 4])
	(d2[k % 4], d2[(k + 1) % 4]) = (d2[(k + 1) % 4], d2[k % 4])
	L.append(''.join(d1))
	L.append(''.join(d2))
	j += 1
	k += 1
if len(L) > len(set(L)):
	print('YES')
else:
	print('NO')


print('YES' if (input() + ''.join(reversed(input()))).replace('X', '') in (input() + ''.join(reversed(input()))).replace('X', '') * 2 else 'NO')


(a, b, c, d) = (input(), input(), input(), input())
a = a + b[::-1]
x = 'X'
for i in range(4):
	if a[i] == x:
		a = a[:i] + a[i + 1:]
		break
c = c + d[::-1]
for i in range(4):
	if c[i] == x:
		c = c[:i] + c[i + 1:]
		break
flag = False
for i in range(4):
	if a == c:
		flag = True
	c = c[1:] + c[0]
if flag:
	print('YES')
else:
	print('NO')


3

def main():
	a = input() + ''.join(reversed(input()))
	b = input() + ''.join(reversed(input()))
	a = a[:a.find('X')] + a[a.find('X') + 1:]
	b = b[:b.find('X')] + b[b.find('X') + 1:]
	for i in range(3):
		if a[i:] + a[:i] == b:
			return True
	return False
if main():
	print('YES')
else:
	print('NO')


a = (input() + input()[::-1]).replace('X', '')
b = (input() + input()[::-1]).replace('X', '')
c = b.index(a[0])
d = b[c:] + b[:c]
print('YES' if a == d else 'NO')


def read_ints():
	return [int(x) for x in input(' ').split()]

def evaluate(top, bot):
	res = 1 if 'X' in top else 2
	perm = top + bot
	perm = [x for x in perm if x != 'X']
	for i in range(len(perm)):
		for j in range(i + 1, len(perm)):
			if perm[i] > perm[j]:
				res += 1
	return res % 2

def main():
	top = input()
	bot = input()
	x = evaluate(top, bot)
	top = input()
	bot = input()
	y = evaluate(top, bot)
	print('YES' if x == y else 'NO')
main()


from collections import deque
f = []
s = []
h = input()
h1 = input()
h = h + h1[::-1]
for i in h:
	if i != 'X':
		f.append(i)
h = input()
h1 = input()
h = h + h1[::-1]
for i in h:
	if i != 'X':
		s.append(i)
f1 = []
f1.append(f[1])
f1.append(f[2])
f1.append(f[0])
f2 = []
f2.append(f[2])
f2.append(f[0])
f2.append(f[1])
if f1 == s or f2 == s or f == s:
	print('YES')
else:
	print('NO')


s = []
for i in range(2):
	a1 = input()
	a2 = input()
	a = a1 + a2
	k = a.index('X')
	if k == 0:
		x = a[2] + a[1] + a[3]
	elif k == 1:
		x = a[2] + a[0] + a[3]
	elif k == 2:
		x = a[3] + a[0] + a[1]
	else:
		x = a[2] + a[0] + a[1]
	s.append(x)
b1 = s[0][1] + s[0][2] + s[0][0]
b2 = s[0][2] + s[0][0] + s[0][1]
if s[0] == s[1] or s[1] == b1 or s[1] == b2:
	print('YES')
else:
	print('NO')


a = input()
b = input()
c = input()
d = input()
s = list(a + b[1] + b[0])
t = list(c + d[1] + d[0])
s.remove('X')
t.remove('X')
for i in range(5):
	t = t[1:] + [t[0]]
	if s == t:
		print('YES')
		break
else:
	print('NO')


s = input()
s1 = input()
s = [s[0], s[1]]
s1 = [s1[0], s1[1]]
ss = s[::-1] + s1
poss = 1
for i in range(4):
	if ss[i] == 'X':
		poss = poss + i
d = input()
d1 = input()
d = [d[0], d[1]]
d1 = [d1[0], d1[1]]
dd = d[::-1] + d1
perest = [[0, 3], [1, 0], [2, 1], [3, 2]]
dv = perest[:poss]
dv = dv[::-1]
dv2 = perest[poss:4]
dv2 = dv2[::-1]
perest = dv + dv2
res = False
for i in range(3):
	for y in range(4):
		(ss[perest[y][1]], ss[perest[y][0]]) = (ss[perest[y][0]], ss[perest[y][1]])
		if ss == dd:
			res = True
if res == True:
	print('YES')
else:
	print('NO')


A = input()
A += input()[::-1]
A = A.replace('X', '')
B = input()
B += input()[::-1]
B = B.replace('X', '')
if B == A or B[1:] + B[0] == A or B[2:] + B[:2] == A:
	print('YES')
else:
	print('NO')


a = [''] * 2
b = [''] * 2
for i in range(2):
	a[i] = input()
for i in range(2):
	b[i] = input()
way1 = ''
way2 = ''
for i in range(2):
	if i == 0:
		for q in range(2):
			if a[i][q] != 'X':
				way1 += a[i][q]
			if b[i][q] != 'X':
				way2 += b[i][q]
	else:
		for q in range(1, -1, -1):
			if a[i][q] != 'X':
				way1 += a[i][q]
			if b[i][q] != 'X':
				way2 += b[i][q]
for i in range(3):
	if way1 == way2:
		print('YES')
		exit()
	way2 = way2[1:] + way2[0]
print('NO')
exit()


l = []
for i in range(2):
	(r, o) = (input(), input())
	r = list(o[0] + r + o[1])
	r.remove('X')
	while r[0] != 'A':
		(r[0], r[1], r[2]) = (r[2], r[0], r[1])
	l.append(''.join(r))
print('YES' if l[0] == l[1] else 'NO')


list1 = [i for i in input()]
list2 = [i for i in input()]
list3 = [i for i in input()]
list4 = [i for i in input()]
chars1 = ''
chars2 = ''
if list1[0] != 'X':
	chars1 += list1[0]
if list1[1] != 'X':
	chars1 += list1[1]
if list2[1] != 'X':
	chars1 += list2[1]
if list2[0] != 'X':
	chars1 += list2[0]
chars1 *= 2
if list3[0] != 'X':
	chars2 += list3[0]
if list3[1] != 'X':
	chars2 += list3[1]
if list4[1] != 'X':
	chars2 += list4[1]
if list4[0] != 'X':
	chars2 += list4[0]
if chars2 in chars1:
	print('YES')
else:
	print('NO')


s1 = str(input()) + str(input())
s2 = str(input()) + str(input())

def f(s):
	return 1 if s in ['ABXC', 'ABCX', 'AXCB', 'XACB', 'CAXB', 'XBAC', 'BXAC', 'BCAX', 'BCXA', 'XCBA', 'CXBA', 'CABX'] else 2
c1 = f(s1)
c2 = f(s2)
print('YES' if c1 == c2 else 'NO')


Bessie = input() + input()
Elsie = input() + input()

def get_permutations(line):
	permutations = [line]
	x_index = line.index('X')
	for i in range(11):
		if x_index == 0:
			line = ''.join((line[2], line[1], line[0], line[3]))
			x_index = 2
		elif x_index == 1:
			line = ''.join((line[1], line[0], line[2], line[3]))
			x_index = 0
		elif x_index == 2:
			line = ''.join((line[0], line[1], line[3], line[2]))
			x_index = 3
		else:
			line = ''.join((line[0], line[3], line[2], line[1]))
			x_index = 1
		permutations.append(line)
	return permutations
is_best_friends_for_life = not set(get_permutations(Bessie)).isdisjoint(get_permutations(Elsie))
print('YES' if is_best_friends_for_life else 'NO')


s1 = input()
s2 = input()
s3 = input()
s4 = input()
s11 = s1 + s2[::-1]
s22 = s3 + s4[::-1]
k1 = ''
k2 = ''
for i in range(4):
	if s11[i] != 'X':
		k1 += s11[i]
for i in range(4):
	if s22[i] != 'X':
		k2 += s22[i]
k1 = k1 * 3
if k2 in k1:
	print('YES')
else:
	print('NO')


a = input()
b = input()
a = a + b[::-1]
a = a.replace('X', '')
i = a.find('A')
a = a[i:] + a[:i]
c = input()
d = input()
c = c + d[::-1]
c = c.replace('X', '')
i = c.find('A')
c = c[i:] + c[:i]
if a == c:
	print('YES')
else:
	print('NO')


def go(lst):
	global cnt, ans, lst2
	cnt += 1
	if lst == lst2:
		ans = 'YES'
	if cnt < 800:
		if lst[0][0] == 23:
			(lst[0][1], lst[0][0]) = (lst[0][0], lst[0][1])
			go(lst)
			(lst[0][1], lst[0][0]) = (lst[0][0], lst[0][1])
		if lst[0][1] == 23:
			(lst[0][1], lst[1][1]) = (lst[1][1], lst[0][1])
			go(lst)
			(lst[0][1], lst[1][1]) = (lst[1][1], lst[0][1])
		if lst[1][1] == 23:
			(lst[1][1], lst[1][0]) = (lst[1][0], lst[1][1])
			go(lst)
			(lst[1][1], lst[1][0]) = (lst[1][0], lst[1][1])
		if lst[1][0] == 23:
			(lst[0][0], lst[1][0]) = (lst[1][0], lst[0][0])
			go(lst)
			(lst[0][0], lst[1][0]) = (lst[1][0], lst[0][0])
		if lst[0][0] == 23:
			(lst[0][0], lst[1][0]) = (lst[1][0], lst[0][0])
			go(lst)
			(lst[0][0], lst[1][0]) = (lst[1][0], lst[0][0])
		if lst[0][1] == 23:
			(lst[0][1], lst[0][0]) = (lst[0][0], lst[0][1])
			go(lst)
			(lst[0][1], lst[0][0]) = (lst[0][0], lst[0][1])
		if lst[1][1] == 23:
			(lst[1][1], lst[0][1]) = (lst[0][1], lst[1][1])
			go(lst)
			(lst[1][1], lst[0][1]) = (lst[0][1], lst[1][1])
		if lst[1][0] == 23:
			(lst[1][1], lst[1][0]) = (lst[1][0], lst[1][1])
			go(lst)
			(lst[1][1], lst[1][0]) = (lst[1][0], lst[1][1])
s1 = input()
ans = 'NO'
cnt = 0
lst1 = [[], []]
lst1[0] = [ord(i) - ord('A') for i in s1]
s1 = input()
lst1[1] = [ord(i) - ord('A') for i in s1]
s1 = input()
lst2 = [[], []]
lst2[0] = [ord(i) - ord('A') for i in s1]
s1 = input()
lst2[1] = [ord(i) - ord('A') for i in s1]
go(lst1)
print(ans)


c = input()
d = input()
A = c + d[::-1]
c = input()
d = input()
B = c + d[::-1]
A = A.replace('X', '')
B = B.replace('X', '')
A = A + A
if A.find(B) == -1:
	print('NO')
else:
	print('YES')


import sys
s = [x.strip().strip('X') for x in sys.stdin.readlines()]
s1 = s[0][::-1] + s[1] + s[0][-1]
s2 = s[2][::-1] + s[3] + s[2][-1]
ch = 1
for i in range(3):
	if s1[i + 1] != s2[s2.find(s1[i]) + 1]:
		ch = 0
print('YES' if ch else 'NO')


a = list(input()) + list(input())
b = list(input()) + list(input())
a = [a[0], a[1], a[3], a[2]]
b = [b[0], b[1], b[3], b[2]]
for i in range(4):
	if a[i] == 'X':
		del a[i]
		break
for i in range(4):
	if b[i] == 'X':
		del b[i]
		break
b = b + b[:]
for i in range(3):
	if b[i:i + 3] == a:
		print('YES')
		break
else:
	for i in range(6, 3, -1):
		if b[i - 3:i] == a:
			print('YES')
			break
	else:
		print('NO')


count = 12
ans = False
first = [list(input())]
first.append(list(input()))
second = [list(input())]
second.append(list(input()))
for i in range(count):
	if first == second:
		ans = True
		break
	if first[0][0] == 'X':
		(first[0][0], first[0][1]) = (first[0][1], first[0][0])
	elif first[0][1] == 'X':
		(first[0][1], first[1][1]) = (first[1][1], first[0][1])
	elif first[1][1] == 'X':
		(first[1][1], first[1][0]) = (first[1][0], first[1][1])
	elif first[1][0] == 'X':
		(first[1][0], first[0][0]) = (first[0][0], first[1][0])
if ans:
	print('YES')
else:
	print('NO')


def seq(s):
	ans = ''
	for i in s:
		if i != 'X':
			ans += i
	while ans[0] != 'A':
		ans = ans[1:] + ans[0]
	return ans
first = input() + input()[::-1]
second = input() + input()[::-1]
while first[0] != second[0]:
	first = first[1:] + first[0]
if seq(first) != seq(second):
	print('NO')
else:
	print('YES')


a = input()
b = input()
c = input()
d = input()
a = (a + b[::-1]).replace('X', '') * 2
c = (c + d[::-1]).replace('X', '')
if a.find(c) != -1:
	print('YES')
else:
	print('NO')


import math, re, sys, string, operator, functools, fractions, collections
sys.setrecursionlimit(10 ** 7)
RI = lambda x=' ': list(map(int, input().split(x)))
RS = lambda x=' ': input().rstrip().split(x)
dX = [-1, 1, 0, 0, -1, 1, -1, 1]
dY = [0, 0, -1, 1, 1, -1, -1, 1]
mod = int(1000000000.0 + 7)
eps = 1e-06
pi = math.acos(-1.0)
MAX = 20
s1 = (input() + input()[::-1]).replace('X', '')
s2 = (input() + input()[::-1]).replace('X', '')
f = 0
for i in range(3):
	if s1 == s2[i:3] + s2[:i]:
		f = 1
print(['NO', 'YES'][f])


t = ''
ans = ''
s = input()
t = t + s
s = input()
t = t + s[::-1]
t = list(t)
s = input()
ans = ans + s
s = input()
ans = ans + s[::-1]
ans = list(ans)
idx = t.index('X')
for i in range(12):
	(t[(idx + i) % 4], t[(idx + i + 1) % 4]) = (t[(idx + i + 1) % 4], t[(idx + i) % 4])
	if t == ans:
		print('YES')
		exit()
print('NO')


s1 = ''
s2 = ''
a = input()
b = input()
c = input()
d = input()
s1 = list(a + b[1] + b[0])
s2 = list(c + d[1] + d[0])
t = s1.index('X')
if t != 3:
	s1 = s1[:t] + s1[t + 1:]
else:
	s1 = s1[:3]
t = s2.index('X')
if t != 3:
	s2 = s2[:t] + s2[t + 1:]
else:
	s2 = s2[:3]
if s1[0] + s1[1] + s1[2] == s2[0] + s2[1] + s2[2] or s1[1] + s1[2] + s1[0] == s2[0] + s2[1] + s2[2] or s1[2] + s1[0] + s1[1] == s2[0] + s2[1] + s2[2]:
	print('YES')
else:
	print('NO')


s1 = input() + input()[::-1]
s2 = input() + input()[::-1]
s1 = s1[:s1.find('X')] + s1[s1.find('X') + 1:]
s2 = s2[:s2.find('X')] + s2[s2.find('X') + 1:]
flag = False
for i in range(3):
	if s1 == s2:
		flag = True
		break
	s1 = s1[-1] + s1[:-1]
print('YES' if flag else 'NO')


s1 = ''
s2 = ''
for i in range(2):
	r = list(input())
	if i == 1:
		r = r[1] + r[0]
	for c in r:
		if c != 'X':
			s1 += c
for i in range(2):
	r = input()
	if i == 1:
		r = r[1] + r[0]
	for c in r:
		if c != 'X':
			s2 += c
s2 *= 2
k = 0
for i in range(3):
	if s1 == s2[i:i + 3]:
		k = 1
if k == 0:
	print('NO')
else:
	print('YES')


a = input()
b = input()[::-1]
s1 = ''
if a[0] != 'X':
	s1 += a[0]
if a[1] != 'X':
	s1 += a[1]
if b[0] != 'X':
	s1 += b[0]
if b[1] != 'X':
	s1 += b[1]
a = input()
b = input()[::-1]
s2 = ''
if a[0] != 'X':
	s2 += a[0]
if a[1] != 'X':
	s2 += a[1]
if b[0] != 'X':
	s2 += b[0]
if b[1] != 'X':
	s2 += b[1]
c = s2.index(s1[0])
if s2[(c + 1) % 3] == s1[1]:
	print('YES')
else:
	print('NO')


L1 = list(input()[::-1] + input())
L2 = list(input()[::-1] + input())
L1.remove('X')
inA1 = L1.index('A')
L2.remove('X')
inA2 = L2.index('A')
L1 = L1[inA1:] + L1[:inA1]
L2 = L2[inA2:] + L2[:inA2]
if L1 == L2:
	print('YES')
else:
	print('NO')


a = input()
b = input()
c = input()
d = input()
if a[0] == 'X':
	s1 = a[1] + b[1] + b[0]
if a[1] == 'X':
	s1 = b[1] + b[0] + a[0]
if b[0] == 'X':
	s1 = a + b[1]
if b[1] == 'X':
	s1 = a + b[0]
if c[0] == 'X':
	s2 = c[1] + d[1] + d[0]
if c[1] == 'X':
	s2 = d[1] + d[0] + c[0]
if d[0] == 'X':
	s2 = c + d[1]
if d[1] == 'X':
	s2 = c + d[0]
s1 = s1 + s1
for i in range(0, len(s1)):
	if s1[i] == s2[0]:
		if s1[i + 1] == s2[1] and s1[i + 2] == s2[2]:
			print('YES')
			break
		else:
			print('NO')
			break


import sys
sys.setrecursionlimit(10000000)
from math import pi
a = list(input())
b = list(input())
ab = a + list(reversed(b))
ab.remove('X')
a = list(input())
b = list(input())
cd = a + list(reversed(b))
cd.remove('X')
cd = cd + cd
friends = False
for i in range(3):
	good = True
	for j in range(3):
		if ab[j] != cd[i + j]:
			good = False
	friends = friends or good
if friends:
	print('YES')
else:
	print('NO')


f = lambda : 'ABC' in 2 * (input() + input()[::-1]).replace('X', '')
print('NO' if f() ^ f() else 'YES')


def main():
	res = []
	for _ in (0, 1):
		(a, b) = input()
		(d, c) = input()
		res.append([x for x in (a, b, c, d) if x != 'X'])
	(a, b) = res
	print(('NO', 'YES')[b[b.index(a[0]) - 1] == a[2]])
main()


letters = {0: 1, 1: 3, 3: 2, 2: 0}

def enum_letters(s):
	start = s.find('A')
	yield s[start]
	for i in range(0, 2):
		start = letters[start]
		if s[start] == 'X':
			start = letters[start]
		yield s[start]

def read_string():
	s = ''
	for c in enum_letters(input() + input()):
		s += c
	return s
a = read_string()
b = read_string()
if a == b:
	print('YES')
else:
	print('NO')


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
a = ip() + ip()[::-1]
b = ip() + ip()[::-1]
x = a.index('A')
arr = ''
brr = ''
while len(arr) != 3:
	if a[x] != 'X':
		arr += a[x]
	x = (x + 1) % 4
x = b.index('A')
while len(brr) != 3:
	if b[x] != 'X':
		brr += b[x]
	x = (x + 1) % 4
print('YES' if arr == brr else 'NO')


p11 = input()
p12 = input()
p21 = input()
p22 = input()
s1 = (p11 + p12[1] + p12[0]).replace('X', '')
s2 = (p21 + p22[1] + p22[0]).replace('X', '')
if s1 == 'ABC' or s1 == 'BCA' or s1 == 'CAB':
	first = True
else:
	first = False
if s2 == 'ABC' or s2 == 'BCA' or s2 == 'CAB':
	second = True
else:
	second = False
if first == second:
	print('YES')
else:
	print('NO')