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: interview

Starter code:

Solutions:
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")

# You lost the game.
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")


#[int(i) for i in input().split()]
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')


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")

print(('YES' if (input() + ''.join(reversed(input()))).replace('X', '') \
        in (input() + ''.join(reversed(input()))).replace('X', '') * 2 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 = [''] * 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")
        return
    way2 = way2[1:] + way2[0]
print("NO")
return

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")


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')


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()

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 = [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")


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')

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')

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):
        #print(s[i:] + s[:i], t[j:] + t[:j])
        if s[i:] + s[:i] == t[j:] + t[:j]:
            print("YES")
            return
print("NO")


a = input()
a = a + input()[::-1]
a = a.replace('X','')
b = input()
b = b + input()[::-1]
b = b.replace('X','')
b = b * 2

if b.find(a) == -1:
	print('NO')
else:
	print('YES')

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')

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')
    


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')

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([''.join(r) for r in 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([''.join(r) for r in 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')


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")


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')

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")

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')

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]
                
            
          
        else:
            if 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')