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


"""
???????????? 4

?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°????????????????????????????????????
   +--+
 ???|???|
+--+--+--+--+
|???|???|???|???|
+--+--+--+--+
 ???|???|
   +--+

"""

class Dice:
    T = 0
    S = 1
    E = 2
    W = 3
    N = 4
    B = 5

    """ ????????????????????? """
    def __init__(self):
        """ ????????????????????? """
        self.side = [1, 2, 3, 4, 5, 6]
        self.top = 0

    def setDice(self, arr):
        """ ??¢?????????????????? """
        self.side = arr[0:6]

    def Turn(self, dir):
        """ ?????????????????¢?????? """
        s = self.side

        if dir == "N":     
            
            t = [s[Dice.S],s[Dice.B],s[Dice.E],s[Dice.W],s[Dice.T],s[Dice.N]]
        elif dir == "S": 
            t = [s[Dice.N],s[Dice.T],s[Dice.E],s[Dice.W],s[Dice.B],s[Dice.S]]
        elif dir == "E": 
            t = [s[Dice.W],s[Dice.S],s[Dice.T],s[Dice.B],s[Dice.N],s[Dice.E]]
        elif dir == "W": 
            t = [s[Dice.E],s[Dice.S],s[Dice.B],s[Dice.T],s[Dice.N],s[Dice.W]]
        elif dir == "L": 
            t = [s[Dice.T],s[Dice.W],s[Dice.S],s[Dice.N],s[Dice.E],s[Dice.B]]
        elif dir == "R": 
            t = [s[Dice.T],s[Dice.E],s[Dice.N],s[Dice.S],s[Dice.W],s[Dice.B]]

        self.side = t


    def Uturns(self, tval, sval):
        """ ?????¢??¨?????¢???????????????????????????????????§?????¢ """
    
        if self.side[Dice.E] == tval:
            self.Turn("W")
        elif self.side[Dice.W] == tval:
            self.Turn("E")
        elif self.side[Dice.N] == tval:
            self.Turn("S")
        elif self.side[Dice.S] == tval:
            self.Turn("N")
        elif self.side[Dice.B] == tval:
            self.Turn("N")
            self.Turn("N")

        for i in range(3): 
            if self.side[Dice.S] == sval:
                break
            self.Turn("L")
            

    def Search(self, tval, sval, dir):
        """ ?????¢??¨?????¢?????????????????????????????¢??????????????? """
        self.Uturns(tval,sval)
        return self.side[dir]

    def State(self):
            return "T0:{0} S1:{1} W2:{2} E3:{3} N4:{4} B5:{5}".format(self.side[0],self.side[1], self.side[2],self.side[3], self.side[4],self.side[5])


d = []
num = int(input().strip())

for i in range(num): 
    tmp = Dice()
    tmp.setDice(list(map(int,input().strip().split())))
    d.append(tmp)

ret = "Yes"

for i in range(num-1):
    for j in range(i + 1,num):
        
        s1 = set(d[i].side)
        s2 = set(d[j].side)
        if (s1 ^ s2): 
            break;

        d[j].Uturns(d[i].side[Dice.T], d[i].side[Dice.S])
        s1 = d[i].State()
        s2 = d[j].State()
        if s1 == s2:
            ret = "No"
            break

    if ret == "No":
        break

print(ret)

Solution (Python):
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def issame(d1,d2):
	for o in 'cccWcccEcccNcccWcccWccc':
		if d1.faces == d2.faces:
			break
		else:
			d2.turn(o)
	if d1.faces == d2.faces:
		return True
	else:
		return False

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	for d1,d2 in combinations(dices,2):
		if issame(d1,d2):
			ans = False
			break
	print({True:'Yes',False:'No'}[ans])

Solution (Python):
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def issame(d1,d2):
	for o in 'cccWcccEcccNcccWcccWccc':
		if d1.faces == d2.faces:
			break
		else:
			d2.turn(o)
	if d1.faces == d2.faces:
		return True
	else:
		return False

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	for d1,d2 in combinations(dices,2):
		if issame(d1,d2):
			ans = False
			break
	print({True:'Yes',False:'No'}[ans])

Solution (Python):
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def isunique(d1,d2):
	for o in 'cccWcccEcccNcccWcccWccc':
		if d1.faces == d2.faces:
			break
		else:
			d2.turn(o)
	if d1.faces == d2.faces:
		return False
	else:
		return True

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	check = [isunique(d1,d2) for d1,d2 in combinations(dices,2)]
	if all(check):
		print('Yes')
	else:
		print('No')

Solution (Python):
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def min_side(dice):
	num = [int(''.join([str(i) for i in dice.faces]))]
	for o in 'cccWcccEcccNcccWcccWccc':
		dice.turn(o)
		num.append(int(''.join([str(i) for i in dice.faces])))
	return min(num)

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	mins1 = [min_side(d) for d in dices]
	mins2 = list(set(mins1))
	if len(mins1) == len(mins2):
		print('Yes')
	else:
		print('No')

Solution (Python):
class Dice:
    __slots__ = ['n_list', 'pair_list']

    def __init__(self, n_tup):
        self.n_list = list(n_tup)
        self.pair_list = [self.return_dice_round_pair(self.n_list, 2, 3, 5, 4),
                          self.return_dice_round_pair(self.n_list, 1, 4, 6, 3),
                          self.return_dice_round_pair(self.n_list, 1, 2, 6, 5),
                          self.return_dice_round_pair(self.n_list, 1, 5, 6, 2),
                          self.return_dice_round_pair(self.n_list, 1, 3, 6, 4),
                          self.return_dice_round_pair(self.n_list, 2, 4, 5, 3)
                          ]

    @staticmethod
    def roll_switch(n_list, from_idx_list, to_idx_list):
        n_list[from_idx_list[0] - 1], \
        n_list[from_idx_list[1] - 1], \
        n_list[from_idx_list[2] - 1], \
        n_list[from_idx_list[3] - 1] \
            = n_list[to_idx_list[0] - 1], \
              n_list[to_idx_list[1] - 1], \
              n_list[to_idx_list[2] - 1], \
              n_list[to_idx_list[3] - 1]
        return n_list

    def roll(self, direction):
        if direction == "N":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [2, 6, 5, 1])

        elif direction == "E":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [4, 1, 3, 6])

        if direction == "S":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [5, 1, 2, 6])

        if direction == "W":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [3, 6, 4, 1])

    def spin(self):
        self.n_list = self.roll_switch(self.n_list, [2, 3, 5, 4], [3, 5, 4, 2])

    @staticmethod
    def return_dice_round_pair(n_list, a, b, c, d):
        return [(n_list[a - 1], n_list[b - 1]),
                (n_list[b - 1], n_list[c - 1]),
                (n_list[c - 1], n_list[d - 1]),
                (n_list[d - 1], n_list[a - 1])]

    def return_right_num(self, top, forward):
        for i in range(6):
            if (top, forward) in self.pair_list[i]:
                return self.n_list[i]


def comparison_dice(dice_src, dice_tmp):
    
    def compare_back(dice_tmp, dice_src):
        if dice_tmp.n_list[5] != dice_src.n_list[5]:
            return False
        else:
            if compare_with_spin(dice_tmp, dice_src):
                return True
            else:
                return False

    def compare_with_spin(dice_tmp, dice_src):
        
        for k in range(4):
            dice_tmp.spin()
            if dice_tmp.n_list == dice_src.n_list:
                return True
        else:
            return False

    
    for i in range(4):
        dice_tmp.roll("W")
        if dice_tmp.n_list[0] == dice_src.n_list[0]:
            if compare_back(dice_tmp, dice_src):
                return True
    else:
        for j in range(4):
            dice_tmp.roll("N")
            if dice_tmp.n_list[0] == dice_src.n_list[0]:
                if compare_back(dice_tmp, dice_src):
                    return True
        else:
            return False


cnt = int(input())
dice_line = []
for i in range(cnt):
    dice_line.append([int(x) for x in input().split()])

flg = False
for j in range(cnt):
    for k in range(j + 1, cnt):
        if comparison_dice(Dice(dice_line[j]), Dice(dice_line[k])):
            print("No")
            flg = True
            break
    if flg: break
else:
    print("Yes")

Solution (Python):
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

def checkdice(die1, die2):
    ans = False
    for d in "RRRNRRRNRRRSRRRSRRRNRRRR":
        if die1 == die2:
            ans = True
            break
        die2 = roll(die2, d)
    return ans

dice = []
msg = "Yes"
n = int(input())
for _ in range(n):
    dice.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i + 1, n):
        if checkdice(dice[i], dice[j]):
            msg = "No"
print(msg)

Solution (Python):
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

def checkdice(die1, die2):
    ans = False
    for d in "RRRNRRRNRRRSRRRSRRRNRRRR":
        if die1 == die2:
            ans = True
            break
        die2 = roll(die2, d)
    return ans

dice = []
msg = "Yes"
n = int(input())
for _ in range(n):
    dice.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i + 1, n):
        if checkdice(dice[i], dice[j]):
            msg = "No"
            break
    if msg == "No":
        break
print(msg)

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")



a = [0,2,5,3]
b = [0,1,5,4]
c = [1,2,4,3]

def sortdice(dice1,dice2,p,q,r):
    for i in range(4):
        dice1[p[0]],dice1[p[1]],dice1[p[2]],dice1[p[3]]=dice1[p[1]],dice1[p[2]],dice1[p[3]],dice1[p[0]]
        for j in range(4):
            dice1[q[0]],dice1[q[1]],dice1[q[2]],dice1[q[3]]=dice1[q[1]],dice1[q[2]],dice1[q[3]],dice1[q[0]]
            for k in range(4):
                dice1[r[0]],dice1[r[1]],dice1[r[2]],dice1[r[3]]=dice1[r[1]],dice1[r[2]],dice1[r[3]],dice1[r[0]]
                if dice1==dice2:
                    return True
    return False

def all_different(N, dices):
    for i in range(N - 1):
        dice1 = dices[i]
        for j in range(i + 1, N):
            dice2 = dices[j]
            if sortdice(dice1, dice2, a, b, c):
                return False
    return True

    
N = int(input())
dices = []
for _ in range(N):
    dices.append(list(map(int,input().split())))
print("Yes" if all_different(N, dices) else "No")

Solution (Python):


def r(D,s):
    if s==0:
        t=D[0]
        D[0]=D[1]
        D[1]=D[5]
        D[5]=D[4]
        D[4]=t
    elif s==2:
        t=D[0]
        D[0]=D[4]
        D[4]=D[5]
        D[5]=D[1]
        D[1]=t        
    elif s==3:
        t=D[0]
        D[0]=D[3]
        D[3]=D[5]
        D[5]=D[2]
        D[2]=t        
    elif s==1:
        t=D[0]
        D[0]=D[2]
        D[2]=D[5]
        D[5]=D[3]
        D[3]=t
    elif s==4:
        D=r(r(D,1),1)
    return D
def ch(C,D):
    k=0
    while k<6 and C[k]==D[k]:
        k+=1
    if k==6:
        return 1
    else:
        return 0
def d3(C,D):
    nd=0
    if ch(C,D)==1:
        nd=1
    i=0
    while i<4 and nd==0:
        D=r(D,i)
        j=i+1
        j%=4
        k=0
        if ch(C,D)==1:
            nd=1
 
    
        while k<3 and nd==0:
            D=r(D,j)
            if ch(C,D)==1:
                nd=1
            k+=1
  
        
        D=r(D,j)
 
   
        m=i+2
        m%=4
 
    
        D=r(D,m)
 
 
        i+=1
    i=0
    while nd==0 and i<4:
        D=r(D,0)
        D=r(D,1)
        D=r(D,0)
        if ch(C,D)==1:
            nd=1
        D=r(r(D,0),0)
        if ch(C,D)==1:
            nd=1
        i+=1
    if nd==1:
        return 1
    else:
        return 0
n=int(input())
Dl=[[int(i) for i in input().split()] for i in range(n)]
i1=0
ne=1
while i1<n-1 and ne==1:
    i2=i1+1
    while i2<n and ne==1:
        if d3(Dl[i1],Dl[i2])==1:
            ne=0
        i2+=1
    i1+=1
if ne==0:
    print("No")
else:
    print("Yes")

Solution (Python):
def North(dice):
    w = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = w
    return dice
def East(dice):
    w = dice[1]
    dice[1] = dice[2]
    dice[2] = dice[4]
    dice[4] = dice[3]
    dice[3] = w
    return dice

def West(dice):
    w = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = w
    return dice

maxloop = 4
Ncnt = 0
Ecnt = 0
Wcnt = 0

n = int(input())
Dice = [['' for i2 in range(6)] for i1 in range(n)]
for i in range(n):
    Dice[i] = input().split()

Flag = False
Nloop = True
Eloop = True
Wloop = True

for i in range(n - 1):
    for j in range(i + 1, n):
        while Eloop:
            while Nloop:
                while Wloop:
                    if Dice[i] == Dice[j]:
                        Flag = True
                        Nloop = False
                        Eloop = False
                        Wloop = False
                        break
                    Dice[i] = West(Dice[i])
                    Wcnt += 1
                    if Wcnt == maxloop:
                        Wloop = False
                Dice[i] = North(Dice[i])
                Wcnt = 0
                Wloop = True
                Ncnt += 1
                if Ncnt == maxloop:
                    Nloop = False
            Dice[i] = East(Dice[i])
            Ncnt = 0
            Nloop = True
            Ecnt += 1
            if Ecnt == maxloop:
                Eloop = False
        Ecnt = 0
        Eloop = True
        if Flag == True:
            print("No")
            break
    else:
        continue
    break

if Flag == False:
    print("Yes")

Solution (Python):
class Checker:
    d = {
        1: { 2: (3,4,5,6), 3: (5,2,4,6), 4: (2,5,3,6), 5: (4,3,2,6) },
        2: { 1: (4,3,6,5), 3: (1,6,4,5), 4: (6,1,3,5), 6: (3,4,1,5) },
        3: { 1: (2,5,6,4), 2: (6,1,5,4), 5: (1,6,2,4), 6: (5,2,1,4) },
        4: { 1: (5,2,6,3), 2: (1,6,5,3), 5: (6,1,2,3), 6: (2,5,1,3) },
        5: { 1: (3,4,6,2), 3: (6,1,4,2), 4: (1,6,3,2), 6: (4,3,1,2) },
        6: { 2: (4,3,5,1), 3: (2,5,4,1), 4: (5,2,3,1), 5: (3,4,2,1) }
        }

    def __init__(self, s):
        l = s.split()
        self.tbl = {}
        for c,i in zip(l, range(1,7)):
            if c in self.tbl:
                self.tbl[c].append(i)
            else:
                self.tbl[c] = [i]
        self.pip = dict(zip(range(1,7), l))

    def __call__(self, s):
        l = s.split()

        if not (l[0] in self.tbl):
            return False
        if not (l[1] in self.tbl):
            return False

        for i0 in self.tbl[l[0]]:
            for i1 in self.tbl[l[1]]:
                if i1 in self.d[i0]:
                    for i,j in zip(self.d[i0][i1], range(2,6)):
                        if self.pip[i] != l[j]:
                            break
                    else:
                        return True
        return False

def g():
    n = int(input()) - 1
    c = Checker(input())
    for _ in range(n):
        yield c(input())

if any(g()):
    print("No")
else:
    print("Yes")

Solution (Python):
class Dice3():
    def __init__(self, v): 
        
        self.dv = [(v[i], v[5-i],
                    ''.join([v[l] for l in ((i+2)%6, (i+4)%6, 5-(i+2)%6, 5-(i+4)%6, (i+2)%6, (i+4)%6, 5-(i+2)%6)]) )
                   for i in  range(6)]
    def IsDifferent(self, d):
        for i in range(6):
            if d[0] != self.dv[i][0] or d[5] != self.dv[i][1]: continue
            if not( ''.join(d[1:3]) in self.dv[i][2]): continue
            p = self.dv[i][2].index( ''.join(d[1:3]) )
            if d[4] != self.dv[i][2][p+2] or d[3] != self.dv[i][2][p+3]: continue
            return False
        return True


dices = [input().split() for n in range(int(input()))]
for i, D1 in enumerate(dices[:-1]):
    d = Dice3(D1)
    for D2 in dices[i+1:]:
        if not d.IsDifferent(D2):
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Solution (Python):
class Dice:
    def __init__(self, faces = None):
        self.dice = {'N':2, 'E':4, 'S':5, 'W':3}
        self.faces = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6}
        self.currTop = 1
        if faces is not None:
            self.faces = faces

    def top(self):
        return self.currTop

    def topFace(self):
        return self.faces[self.currTop]

    def bottom(self):
        return 7 - self.currTop

    def bottomFace(self):
        return self.faces[self.bottom()]

    def rot(self, direction):
        newTop = self.dice[direction]
        currTop = self.currTop
        self.currTop = newTop
        if direction == 'N':
            self.dice['N'] = 7 - currTop
            self.dice['S'] = currTop
        elif direction == 'S':
            self.dice['N'] = currTop
            self.dice['S'] = 7 - currTop
        elif direction == 'E':
            self.dice['E'] = 7 - currTop
            self.dice['W'] = currTop
        elif direction == 'W':
            self.dice['E'] = currTop
            self.dice['W'] = 7 - currTop

    def yaw(self, direction):
        newDice = {}
        if direction == 'E':
            newDice['N'] = self.dice['E']
            newDice['E'] = self.dice['S']
            newDice['S'] = self.dice['W']
            newDice['W'] = self.dice['N']
        if direction == 'W':
            newDice['N'] = self.dice['W']
            newDice['E'] = self.dice['N']
            newDice['S'] = self.dice['E']
            newDice['W'] = self.dice['S']
        self.dice = newDice

def compareDice(d1, d2):
    d2indexes = [d2index for d2index,d2face in d2.faces.items() if d1.topFace() == d2face]

    for d2topIndex in d2indexes:
        if dbg: print('d2topIndex:%d' % (d2topIndex) )

        
        key = [k for k,v in d2.dice.items() if d2topIndex == v]
        if len(key) == 0:
            d2.rot('N')
            key = [k for k,v in d2.dice.items() if d2topIndex == v]
        d2.rot(key[0])

        if d1.topFace() != d2.topFace() or d1.bottomFace() != d2.bottomFace():
            continue

        
        cnt = 0
        while True:
            if dbg: print('cnt:%d' % (cnt) )
            if cnt > 4:
                break
            d2.yaw('E')
            cnt += 1

            
            d1State = {k:d1.faces[v] for k,v in d1.dice.items()}
            d2State = {k:d2.faces[v] for k,v in d2.dice.items()}
            if d1State == d2State:
                return True

    return False

dbg = False

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

for i in range(n):
    faces = {k:v for k,v in zip(range(1,7), map(int, input().split()))}
    dices.append(Dice(faces))

for i in range(n):
    for j in range(i+1,n):
        if dbg: print('%d-%d' % (i,j) )
        ret = compareDice(dices[i], dices[j])
        if dbg: print(ret)
        if ret == True:
            print('No')
            exit()

print('Yes')

Solution (Python):
def main_1_11_A():
    values = list(map(int, input().split()))
    query = input()

    dice = Dice(values)
    for d in query:
        dice.roll(d)


def main_1_11_B():
    values = list(map(int, input().split()))
    n = int(input())

    for _ in range(n):
        t_value, s_value = map(int, input().split())

        print(_B(values, t_value, s_value))


def main_1_11_C():
    values_1 = list(map(int, input().split()))
    values_2 = list(map(int, input().split()))

    dice1 = Dice(values_1)
    dices = (
        make_dices(values_2) +
        list(map(lambda dice: rot_n(dice, 1), make_dices(values_2))) +
        list(map(lambda dice: rot_n(dice, 2), make_dices(values_2))) +
        list(map(lambda dice: rot_n(dice, 3), make_dices(values_2)))
    )
    for dice in dices:
        for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
            dice.roll(d)
            if eq_dice(dice1, dice):
                print("Yes")
                return

    print("No")


def main_1_11_D():
    n = int(input())
    values_list = [
        list(map(int, input().split()))
        for _ in range(n)
    ]

    
    for i in range(n):
        dice1 = Dice(values_list[i])
        for j in range(i + 1, n):
            values_2 = values_list[j]

            dices = (
                make_dices(values_2) +
                list(map(lambda dice: rot_n(dice, 1), make_dices(values_2))) +
                list(map(lambda dice: rot_n(dice, 2), make_dices(values_2))) +
                list(map(lambda dice: rot_n(dice, 3), make_dices(values_2)))
            )
            for dice in dices:
                for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
                    dice.roll(d)
                    if eq_dice(dice1, dice):
                        print("No")
                        return

    print("Yes")


def eq_dice(d1, d2):
    return (
        d1.top_value == d2.top_value and
        d1.bottom_value == d2.bottom_value and
        d1.north_value == d2.north_value and
        d1.south_value == d2.south_value and
        d1.west_value == d2.west_value and
        d1.east_value == d2.east_value
    )


def rot_n(dice, n):
    for _ in range(n):
        dice.roll("N").roll("E").roll("S")
    return dice


def make_dices(values):
    
    return [
        Dice(values),
        Dice(values).roll("N"),
        Dice(values).roll("N").roll("N"),
        Dice(values).roll("N").roll("N").roll("N"),
        Dice(values).roll("E"),
        Dice(values).roll("W"),
    ]


def _B(values, t_value, s_value):
    """
    ????????¢?????????????????¨??????
    ??´??¢???????????¢?????????????????§???????????????????????¨?????????
    ??????????????????????????¢???????¢??????????
    """
    dices = (
        make_dices(values) +
        list(map(lambda dice: rot_n(dice, 1), make_dices(values))) +
        list(map(lambda dice: rot_n(dice, 2), make_dices(values))) +
        list(map(lambda dice: rot_n(dice, 3), make_dices(values)))
    )
    for dice in dices:
        for d in ("E" * 4 + "W" * 4 + "S" * 4 + "N" * 4):
            dice.roll(d)
            if (dice.top_value, dice.south_value) == (t_value, s_value):
                return dice.east_value


class Dice:

    def __init__(self, values):
        self.values = values
        
        self.t = 1
        self.b = 6
        self.w = 4
        self.e = 3
        self.n = 5
        self.s = 2

    def __repr__(self):
        labels = {
            "t": self.t,
            "b": self.b,
            "w": self.w,
            "e": self.e,
            "n": self.n,
            "s": self.s,
        }
        return "<%s (%s, %s)>" % (self.__class__, self.values, labels)

    def roll(self, direction):
        if direction == "E":
            after_lables = self.t, self.b, self.e, self.w
            self.e, self.w, self.b, self.t = after_lables
        elif direction == "W":
            after_lables = self.t, self.b, self.e, self.w
            self.w, self.e, self.t, self.b = after_lables
        elif direction == "S":
            after_lables = self.t, self.b, self.n, self.s
            self.s, self.n, self.t, self.b = after_lables
        elif direction == "N":
            after_lables = self.t, self.b, self.n, self.s
            self.n, self.s, self.b, self.t = after_lables

        return self

    @property
    def bottom_value(self):
        return self.values[self.b - 1]

    @property
    def north_value(self):
        return self.values[self.n - 1]

    @property
    def west_value(self):
        return self.values[self.w - 1]

    @property
    def top_value(self):
        return self.values[self.t - 1]

    @property
    def south_value(self):
        return self.values[self.s - 1]

    @property
    def east_value(self):
        return self.values[self.e - 1]

if __name__ == "__main__":
    
    
    
    main_1_11_D()

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

def getRound(D, top):
	result = []
	for index in dice[top]:
		result.append(D[index])
	return result

def compareRound(l1, l2):
	if l1[4] != l2[4]:
		return False
	elif l1[0] not in l2:
		return False
	offset = l2.index(l1[0])
	
	for i in range(4):
		if l1[i] != l2[(i+offset)%4]:
			return False
	return True

def compare(D1, D2, i1):
	if D1[i1] not in D2:
		return False
	i2 = D2.index(D1[i1])
	return compareRound(getRound(D1, i1), getRound(D2, i2))

def isSame(D1, D2):
	for i in range(6):
		if compare(D1, D2, i):
			return True
	return False

def isAllDifferent(DList, n):
	for i in range(n-1):
		for j in range(i+1, n):
			if isSame(DList[i], DList[j]):
				return False
	return True

n = int(input())
DList = []
for i in range(n):
	DList.append(input().split(" "))


if isAllDifferent(DList, n):
	print("Yes")
else:
	print("No")

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

Solution (Python):
import random


class Dice(object):
    def __init__(self):
        self.t = 1
        self.s = 2
        self.e = 3
        self.w = 4
        self.n = 5
        self.b = 6

    def __init__(self, faces):
        self.t = faces[0]
        self.s = faces[1]
        self.e = faces[2]
        self.w = faces[3]
        self.n = faces[4]
        self.b = faces[5]

    def roll(self, direct):
        if direct == 0:
            self.t, self.s, self.b, self.n = self.n, self.t, self.s, self.b
        elif direct == 1:
            self.t, self.w, self.b, self.e = self.e, self.t, self.w, self.b
        elif direct == 2:
            self.n, self.w, self.s, self.e = self.w, self.s, self.e, self.n

    def is_equal(self, dice):
        if self.t == dice.t and self.s == dice.s and self.e == dice.e and self.w == dice.w and self.n == dice.n and self.b == dice.b:
            return True
        else:
            return False

    def get_nums(self):
        return {self.t, self.s, self.e, self.w, self.n, self.b}

n = int(input())
dice_list = []
flag_list = []
for _ in range(n):
    flag = False
    faces = list(map(int, input().split()))
    dice_list.append(Dice(faces))

    if n == 0:
        continue

    dice_a = dice_list[-1]
    for i in range(len(dice_list) - 1):
        dice_b = dice_list[i]

        if (dice_a.get_nums() == dice_b.get_nums()):
            for i in range(1000):
                dice_b.roll(random.randint(0, 3))
                if dice_a.is_equal(dice_b):
                    flag = True
                    break

        flag_list.append(flag)

if any(flag_list):
    print('No')
else:
    print('Yes')

Solution (Python):
mask = {
    'N': (1, 5, 2, 3, 0, 4),
    'R': (0, 2, 4, 1, 3, 5)
    }


def isIdentifyDice(dice, dice2):
    find = False

    for i in range(4):

        for j in range(4):
            if dice == dice2:
                find = True
                break
            dice = [dice[x] for x in mask['R']]

        if dice == dice2:
            find = True
            break
        dice = [dice[x] for x in mask['N']]

    dice = [dice[x] for x in mask['R']]
    dice = [dice[x] for x in mask['N']]
    for j in range(4):
        if dice == dice2:
            find = True
            break
        dice = [dice[x] for x in mask['R']]

    dice = [dice[x] for x in mask['N']]
    dice = [dice[x] for x in mask['N']]
    for j in range(4):
        if dice == dice2:
            find = True
            break
        dice = [dice[x] for x in mask['R']]

    return find


n = int(input())
dt = list(map(int, input().split()))

chk = True
for i in range(n - 1):
    dc = list(map(int, input().split()))
    chk = not isIdentifyDice(dc, dt)
    if not chk:
        break

print("Yes" if chk else "No")