Problem:
Dice IV Write a program which reads $n$ dices constructed in the same way as Dice I , and determines whether they are all different. For the determination, use the same way as Dice III . Input In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III . Output Print " Yes " if given dices are all different, otherwise " No " in a line. Constraints  $2 \leq n \leq 100$  $0 \leq $ the integer assigned to a face $ \leq 100$  Sample Input 1 3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1 Sample Output 1 No Sample Input 2 3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6 Sample Output 2 Yes

Solution (Python):

class Dice:
    def __init__(self, v1, v2, v3, v4, v5, v6):
        self.faces = [(v1, v6), (v2, v5), (v3, v4)]

    def rotate_left(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(top, rgt, bck, fnt, lft, btm)

    def rotate_right(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(top, lft, fnt, bck, rgt, btm)

    def turn_back(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(fnt, btm, rgt, lft, top, bck)

    def turn_front(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(bck, top, rgt, lft, btm, fnt)

    def turn_right(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(lft, fnt, top, btm, bck, rgt)

    def turn_left(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return self.__class__(rgt, fnt, btm, top, bck, lft)

    def __hash__(self):
        (top, btm), (fnt, bck), (rgt, lft) = self.faces
        return hash(frozenset((frozenset((top, btm)),
                               frozenset((fnt, bck)),
                               frozenset((rgt, lft)))))

    def __str__(self):
        return "<{}>".format(",".join([str(t) for t in self.faces]))

    def __eq__(self, other):
        def equals(d1, d2):
            return d1.faces == d2.faces

        (top, btm) = self.faces[0]
        if (top, btm) in other.faces:
            return (equals(self, other)
                    or equals(self.rotate_right(), other)
                    or equals(self.rotate_left(), other)
                    or equals(self.turn_right(), other)
                    or equals(self.turn_front(), other)
                    or equals(self.rotate_left().rotate_left(), other)
                    or equals(self.turn_back().rotate_left(), other)
                    or equals(self.turn_front().rotate_right(), other)
                    or equals(self.turn_right().rotate_left(), other)
                    or equals(self.turn_left().rotate_right(), other)
                    or equals(self.turn_right().turn_back().turn_back(),
                              other)
                    or equals(self.turn_front().turn_right().turn_right(),
                              other))
        elif (btm, top) in other.faces:
            return self.turn_front().turn_front() == other
        else:
            return False


def run():
    count = int(input())
    dices = set()

    for _ in range(count):
        dice = Dice(*[int(i) for i in input().split()])
        if dice in dices:
            print("No")
            break
        dices.add(dice)
    else:
        print("Yes")


if __name__ == '__main__':
    run()

Solution (Python):
p = [(-1, 2, 4, 1, 3, -1), (3, -1, 0, 5, -1, 2), (1, 5, -1, -1, 0, 4),
(4, 0, -1, -1, 5, 1), (2, -1, 5, 0, -1, 3), (-1, 3, 1, 4, 2, -1)]

def is_equal(d, a):
  ts = [i for i, x in enumerate(d) if x == a[0]]
  fs = [i for i, x in enumerate(d) if x == a[1]]
  b = 0
  for t in ts:
    for f in fs:
      if t == f: continue
      r = p[t][f]
      if r == -1: continue
      l = [d[t], d[f], d[r], d[5-r], d[5-f], d[5-t]]
      if a == l: b += 1; break
  return 1 if b > 0 else 0

l = []
b = 0
for _ in range(int(input())):
  l += [input().split()]
for i in range(0, len(l)):
  for j in range(i+1, len(l)):
    b += is_equal(l[i], l[j])
    if b > 0: break
print("No") if b > 0 else print("Yes")

Solution (Python):
class Dice4():
    def check(self,dice,n):
        count=0
        for i in range(n):
            for j in range(n):
                if i != j:
                    for k in range(4):
                        dice[i][0],dice[i][1],dice[i][5],dice[i][4]=dice[i][4],dice[i][0],dice[i][1],dice[i][5]
                        for l in range(4):
                            dice[i][0],dice[i][2],dice[i][5],dice[i][3]=dice[i][3],dice[i][0],dice[i][2],dice[i][5]
                            for m in range(4):
                                dice[i][1],dice[i][2],dice[i][4],dice[i][3]=dice[i][2],dice[i][4],dice[i][3],dice[i][1]
                                if dice[i] == dice[j]:
                                    count+=1
        return count


Dice=[]
n = int(input())
for i in range(n):
    Dice.append([int(i) for i in input().split()])
dc4 = Dice4()
counter = dc4.check(Dice,n)
if counter == 0:
    print('Yes')
else :
    print('No')

Solution (Python):
class Dice:
    def __init__(self):
        self.u=1
        self.w=2
        self.s=3
        self.e=4
        self.n=5
        self.d=6
        self.dic={"W":0,"S":1,"E":2,"N":3}
      
    def __init__(self,u,w,s,e,n,d):
        self.u=u
        self.w=w
        self.s=s
        self.e=e
        self.n=n
        self.d=d
        self.dic={"W":0,"S":1,"E":2,"N":3}
  
    def rot(self,way):
        if isinstance(way,str):
            way=self.dic[way]
  
        if(way==0):
            c=self.u
            self.u=self.e
            self.e=self.d
            self.d=self.w
            self.w=c
        elif way==1:
            c=self.u
            self.u=self.n
            self.n=self.d
            self.d=self.s
            self.s=c
        elif way==2:
            c=self.u
            self.u=self.w
            self.w=self.d
            self.d=self.e
            self.e=c
    def get_nums(self):   
        return {self.u,self.w,self.s,self.e,self.n,self.w,self.d}
 
def mk_dice():
    u,s,e,w,n,d=map(int,input().split())
    return Dice(u,w,s,e,n,d)
  
import random
q=int(input())
dice_col=[]
ans=True
for j in range(q):
    dice_b=mk_dice()
    for dice in dice_col:
        if(dice.get_nums()==dice_b.get_nums()):
            for i in range (1000):
                dice.rot(random.randint(0,2))
                if(dice.u==dice_b.u and dice.d==dice_b.d and dice.w==dice_b.w and dice.s==dice_b.s and dice.e==dice_b.e and dice.n==dice_b.n):
                    ans=False
        if ~ans:
            break
    dice_col.append(dice_b)
  
if ans:
    print('Yes')
else:
    print('No')

Solution (Python):
class Dice:
    TOP, SOUTH, EAST, WEST, NORTH, BOTTOM = 0, 1, 2, 3, 4, 5
    def __init__(self, d):
        self.dice = d[:]
        self.top = d[0]
        self.south = d[1]
        self.east = d[2]
        self.west = d[3]
        self.north = d[4]
        self.bottom = d[5]

    def __eq__(self, other):
        a = self.permutations()
        a.sort()

        d2 = Dice(other.dice)
        b = d2.permutations()
        b.sort()

        return a == b

    def permutations(self):
        copy = self.dice[:]
        perm = []
        for d in [(), ("S"), ("N"), ("E"), ("W"), ("W", "W")]:
            t = Dice(copy)
            for m in d:
                t.move(m)

            perm.append(t.dice[:])
            for i in range(3):
                t.move("R")
                perm.append(t.dice[:])

        return perm
        
    def set_top_front(self, top, front):
        for i, n in enumerate(self.dice):
            if n == top:
                if i == Dice.TOP: pass
                elif i == Dice.SOUTH: self.move("N")
                elif i == Dice.EAST: self.move("W")
                elif i == Dice.WEST: self.move("E")
                elif i == Dice.NORTH: self.move("S")
                elif i == Dice.BOTTOM:
                    self.move("S")
                    self.move("S")

                break
        else:
            return False
        
        for i, n in enumerate(self.dice):
            if n == front:
                if i == Dice.TOP: return False
                elif i == Dice.SOUTH: pass
                elif i == Dice.EAST: self.move("R")
                elif i == Dice.WEST: self.move("L")
                elif i == Dice.NORTH:
                    self.move("R")
                    self.move("R")
                elif i == Dice.BOTTOM: return False

                break
        else:
            return False

        return True
        
    def move(self, d):
        if d == "N":
            self.move_north()
        elif d == "W":
            self.move_west()
        elif d == "S":
            self.move_south()
        elif d == "E":
            self.move_east()
        elif d == "R":
            self.move_right()
        elif d == "L":
            self.move_left()

        self.dice = [
            self.top,
            self.south,
            self.east,
            self.west,
            self.north,
            self.bottom,
        ]

    def move_right(self):
        self.north, self.west, self.south, self.east \
            = self.west, self.south, self.east, self.north

    def move_left(self):
        self.north, self.west, self.south, self.east \
            = self.east, self.north, self.west, self.south

    def move_north(self):
        self.top, self.south, self.bottom, self.north \
            = self.south, self.bottom, self.north, self.top

    def move_south(self):
        self.top, self.south, self.bottom, self.north \
            = self.north, self.top, self.south, self.bottom

    def move_east(self):
        self.top, self.east, self.bottom, self.west \
            = self.west, self.top, self.east, self.bottom

    def move_west(self):
        self.top, self.east, self.bottom, self.west \
            = self.east, self.bottom, self.west, self.top

if __name__ == "__main__":

    def ne_all():
        import re
        n = int(input().strip())
        dices = []
        for i in range(n):
            nums = [int(n) for n in re.split(r"\s+", input().strip())]
            current = Dice(nums)
            for d in dices:
                if current == d:
                    return False
            dices.append(current)
        else:
            return True

    if ne_all():
        print("Yes")
    else:
        print("No")

Solution (Python):
def north(list):
    List = []
    List.append(list[1])
    List.append(list[5])
    List.append(list[2])
    List.append(list[3])
    List.append(list[0])
    List.append(list[4])
    return List

def west(list):
    List = []
    List.append(list[2])
    List.append(list[1])
    List.append(list[5])
    List.append(list[0])
    List.append(list[4])
    List.append(list[3])
    return List
    
def south(list):
    List = []
    List.append(list[4])
    List.append(list[0])
    List.append(list[2])
    List.append(list[3])
    List.append(list[5])
    List.append(list[1])
    return List

def east(list):
    List = []
    List.append(list[3])
    List.append(list[1])
    List.append(list[0])
    List.append(list[5])
    List.append(list[4])
    List.append(list[2])
    return List

def sort123(DD):
    D = [DD]
    for i in range(0, 3):
        D.append(north(D[i]))
        D.append(south(west(D[0])))
        D.append(south(east(D[0])))
    for i in range(6):
        for j in range(3):
            if j == 0:
                D.append(east(D[i + j]))
            else:
                D.append(east(D[5 + 3 * i + j]))
    D.sort()
    return D[0]

n = int(input())
dice = []
for i in range(n):
    dice.append(sort123(list(map(int, input().split()))))
key = 0
for i in range(0, len(dice)):
    if key == 1: break
    for j in range(i + 1, len(dice)):
        if dice[i] == dice[j]:
            print('No')
            key = 1
            break
if key == 0:
    print('Yes')

Solution (Python):



class dice():
    def __init__(self, arr):
        self.top = arr[0]
        self.south = arr[1]
        self.east = arr[2]
        self.west = arr[3]
        self.north = arr[4]
        self.bottom = arr[5]

    def rotate(self, ope):
        if ope == 'S':
            self.top, self.north, self.bottom, self.south = self.north, self.bottom, self.south, self.top
        elif ope == 'N':
            self.top, self.south, self.bottom, self.north = self.south, self.bottom, self.north, self.top
        elif ope == 'E':
            self.top, self.west, self.bottom, self.east = self.west, self.bottom, self.east, self.top
        elif ope == 'W':
            self.top, self.east, self.bottom, self.west = self.east, self.bottom, self.west, self.top
        elif ope == 'R': 
            self.south, self.east, self.north, self.west = self.east, self.north, self.west, self.south
        elif ope == 'L': 
            self.south, self.east, self.north, self.west = self.west, self.north, self.east, self.south
        else:
            pass

    def get_surfaces(self):
        return [self.top, self.south, self.east, self.north, self.west, self.bottom]

def is_same_dice(dc1, dc2):
    judged = False
    if len((dc1.get_surfaces() and dc2.get_surfaces())) == 6:
        opes = ['', 'S', 'E', 'S', 'E', 'S']
        for op in opes:
            dc2.rotate(op)
            for i in range(4):
                if dc1.get_surfaces() == dc2.get_surfaces():
                    judged = True
                    break
                dc2.rotate('R')
            if judged: break
    return judged

n = int(input())
dices = [dice([int(x) for x in input().split(' ')]) for i in range(n)]

judged = True
for i in range(n-1):
    for j in range(i+1, n):
        if is_same_dice(dices[i], dices[j]):
            judged = False

if judged:
    print('Yes')
else:
    print('No')

Solution (Python):
from itertools import*
n=int(input())
a=[[s for s in input().split()]for _ in range(n)]
for b in a:
 b[3:5]=b[4],b[3]
t=0
for d,e in combinations(a,2):
 for p in('012345','152043','215304','302541','410352','514320'):
  f=[d[int(i)]for i in p]
  if f[0]==e[0]and f[5]==e[5]:
   f=f[1:5]*2
   for k in range(4):
    if f[k:k+4]==e[1:5]:t=1
print(['Yes','No'][t])

Solution (Python):
from itertools import*
n=int(input())
a=[[s for s in input().split()]for _ in range(n)]
for b in a:
 b[3:5]=b[4],b[3]
t=0
for d,e in combinations(a,2):
 for p in('012345','152043','215304','302541','410352','514320'):
  f=[d[int(i)]for i in p]
  if f[0]==e[0]and f[5]==e[5]:
   f=f[1:5]*2
   for k in range(4):
    if f[k:k+4]==e[1:5]:t=1
print(['Yes','No'][t])

Solution (Python):
from itertools import*
def g():
 n=int(input())
 a=[[s for s in input().split()]for _ in range(n)]
 for b in a:
  b[3:5]=b[4],b[3]
 t=0
 for d,e in combinations(a,2):
  for p in('012345','152043','215304','302541','410352','514320'):
   f=[d[int(i)]for i in p]
   if f[0]==e[0]and f[5]==e[5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:t=1
 print(['Yes','No'][t])
g()

Solution (Python):
from itertools import*
def g():
 n=int(input())
 a=[[s for s in input().split()]for _ in range(n)]
 for b in a:
  b[3:5]=b[4],b[3]
 t=0
 for d,e in combinations(a,2):
  for p in('012345','152043','215304','302541','410352','514320'):
   f=[d[int(i)]for i in p]
   if f[0]==e[0]and f[5]==e[5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:t=1
 print(['Yes','No'][t])
g()

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def f():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[0]==e[0]and f[5]==e[5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(f())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def f():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[0]==e[0]and f[5]==e[5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(f())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def f():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[0]==e[0]and f[5]==e[5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(f())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def g():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[0::5]==e[0::5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(g())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def g():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[0::5]==e[0::5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(g())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def g():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[::5]==e[::5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(g())

Solution (Python):
from itertools import*
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:
 b[3:5]=b[4],b[3]
def g():
 for d,e in combinations(a,2):
  for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
   f=[d[i]for i in p]
   if f[::5]==e[::5]:
    f=f[1:5]*2
    for k in range(4):
     if f[k:k+4]==e[1:5]:return'No'
 return'Yes'
print(g())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in('012345','152043','215304','302541','410352','514320'):
    f=[d[int(i)]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in('012345','152043','215304','302541','410352','514320'):
    f=[d[int(i)]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    f=[d[i]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    f=[d[i]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    f=[d[i]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
import sys
b=0
n=int(input())
a=[e.split()for e in sys.stdin]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    f=[d[i]for i in p]
    if f[::5]==a[c][::5]:
     f=f[1:5]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
import sys
b=0
n=int(input())
a=[e.split()for e in sys.stdin]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    if[d[p[0]],d[p[5]]]==a[c][::5]:
     f=[d[i]for i in p[1:5]]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    if[d[p[0]],d[p[5]]]==a[c][::5]:
     f=[d[i]for i in p[1:5]]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
b=0
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    if[d[p[0]],d[p[5]]]==a[c][::5]:
     f=[d[i]for i in p[1:5]]*2
     for k in range(4):
      if f[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):
def f():
 for c in range(n):
  for d in a[c+1:]:
   for p in([0,1,2,3,4,5],[1,5,2,0,4,3],[2,1,5,3,0,4],[3,0,2,5,4,1],[4,1,0,3,5,2],[5,1,4,3,2,0]):
    if[d[p[0]],d[p[5]]]==a[c][::5]:
     b=[d[i]for i in p[1:5]]*2
     for k in range(4):
      if b[k:k+4]==a[c][1:5]:return"No"
 return"Yes"
n=int(input())
a=[input().split()for _ in[0]*n]
for b in a:b[3:5]=b[4],b[3]
print(f())

Solution (Python):



class Dice:
    def __init__(self, n):
        self.upper    = n[0]
        self.backward = n[1]
        self.right    = n[2]
        self.left     = n[3]
        self.ahead    = n[4]
        self.bottom   = n[5]

    def __eq__(self, other):
        return self.upper == other.upper and self.backward == other.backward and self.right == other.right \
               and self.left == other.left and self.ahead == other.ahead and self.bottom == other.bottom

    def roll_north(self):
        self.upper, self.ahead, self.bottom, self.backward = self.backward, self.upper, self.ahead, self.bottom

    def roll_south(self):
        self.upper, self.ahead, self.bottom, self.backward = self.ahead, self.bottom, self.backward, self.upper

    def roll_east(self):
        self.upper, self.right, self.bottom, self.left = self.left, self.upper, self.right, self.bottom

    def roll_west(self):
        self.upper, self.right, self.bottom, self.left = self.right, self.bottom, self.left, self.upper

    def roll_side(self):
        self.ahead, self.right, self.backward, self.left = self.left, self.ahead, self.right, self.backward


def match_dice(dice1, dice2):
    for i in range(4):
        dice1.roll_north()

        for j in range(4):
            dice1.roll_east()

            for k in range(4):
                dice1.roll_side()

                if dice2 == dice1:
                    return 0
    return 1


n = int(input())
dice_list = []
result = []

for i in range(n):
    dice_list.append(Dice(list(map(int, input().split()))))

for i in range(n):
    for j in range(n):
        if i != j:
            result.append(match_dice(dice_list[i], dice_list[j]))

if 0 in result:
    print('No')
else:
    print('Yes')

Solution (Python):
import sys

class Dice:
    def __init__(self, labels):
        self.labels = labels
    def east(self):
        self.labels[0], self.labels[2], self.labels[5], self.labels[3] = self.labels[3], self.labels[0], self.labels[2], self.labels[5]
    def west(self):
        self.labels[0], self.labels[2], self.labels[5], self.labels[3] = self.labels[2], self.labels[5], self.labels[3], self.labels[0]
    def north(self):
        self.labels[0], self.labels[1], self.labels[5], self.labels[4] = self.labels[1], self.labels[5], self.labels[4], self.labels[0]
    def south(self):
        self.labels[0], self.labels[1], self.labels[5], self.labels[4] = self.labels[4], self.labels[0], self.labels[1], self.labels[5]

dice_num = int(input())
dices = []

for _ in range(dice_num):
    dices.append( Dice(list(map(int, input().split()))) )

south_cnt = 0

for d_i1 in range(len(dices)):
    for d_i2 in range(len(dices)):
        if d_i1 == d_i2:
            continue
        dice1 = dices[d_i1]
        dice2 = dices[d_i2]
        for i in range(8):
            for j in range(4):    
                if dice1.labels[0] == dice2.labels[0] and dice1.labels[1] == dice2.labels[1] and dice1.labels[2] == dice2.labels[2] and dice1.labels[3] == dice2.labels[3] and dice1.labels[4] == dice2.labels[4] and dice1.labels[5] == dice2.labels[5]:
                    print("No")
                    sys.exit()

                dice2.south()
                dice2.east()
                dice2.north()

            if south_cnt == 4:
                dice2.east()
            else:
                dice2.south()
                south_cnt += 1
    
print("Yes")

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.t = t
        self.f = f
        self.r = r
        self.l = l
        self.b = b
        self.u = u
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
s='SSSWEEE'
w='YYYY'
ans='Yes'
for u in range(n-1):
    for t in range(u+1,n):
        for j in s:
            for r in w:
                if dice.a[t]==dice.a[u]:
                    ans='No'
                    break
                dice.roll(r,t)
            else:
                dice.roll(j,t)
                continue
            break
        else:
            continue
        break
    else:
        continue
    break
print(ans)

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
s='SSSWEEE'
w='YYYY'
ans='Yes'
for u in range(n-1):
    for t in range(u+1,n):
        for j in s:
            for r in w:
                if dice.a[t]==dice.a[u]:
                    ans='No'
                    break
                dice.roll(r,t)
            else:
                dice.roll(j,t)
                continue
            break
        else:
            continue
        break
    else:
        continue
    break
print(ans)

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
s='SSSWEEE'
w='YYYY'
try:
    for u in range(n - 1):
        for t in range(u + 1, n):
            for j in s:
                for r in w:
                    if dice.a[t] == dice.a[u]:
                        raise Exception
                    dice.roll(r, t)
                dice.roll(j, t)
    else:
        print('Yes')
except Exception:
    print('No')

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
s='SSSWEEE'
w='YYYY'
try:
    for u in range(n - 1):
        for t in range(u + 1, n):
            for j in s:
                for r in w:
                    if dice.a[t] == dice.a[u]:
                        raise Exception
                    dice.roll(r, t)
                dice.roll(j, t)
    else:
        print('Yes')
except:
    print('No')

Solution (Python):
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'U':(5,4,2,3,1,0)}
    def dice_tuple(self):
        self.b=tuple(self.a)
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
    def roll2(self,d,n):
        self.a[n]=[self.b[n][i] for i in self.direction[d]]
    def roll_w(self, n):
        self.a[n] = [self.a[n][i] for i in (0, 3, 1, 4, 2, 5)]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
dice.dice_tuple()
s='SNEWUU'
try:
    for u in range(n - 1):
        for t in range(u + 1, n):
            for j in s:
                for r in range(4):
                    if dice.a[t] == dice.a[u]:
                        raise Exception
                    dice.roll_w(t)
                dice.roll2(j, t)
    else:
        print('Yes')
except:
    print('No')