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, faces):
        self.faces = tuple(faces)
 
    def roll_north(self):
        self.faces = (self.faces[1], self.faces[5], self.faces[2],
                      self.faces[3], self.faces[0], self.faces[4])
 
    def roll_south(self):
        self.faces = (self.faces[4], self.faces[0], self.faces[2],
                      self.faces[3], self.faces[5], self.faces[1])
 
    def roll_west(self):
        self.faces = (self.faces[2], self.faces[1], self.faces[5],
                      self.faces[0], self.faces[4], self.faces[3])
 
    def roll_east(self):
        self.faces = (self.faces[3], self.faces[1], self.faces[0],
                      self.faces[5], self.faces[4], self.faces[2])
 
    def rotate(self):
        self.faces = (self.faces[0], self.faces[2], self.faces[4],
                      self.faces[1], self.faces[3], self.faces[5])
 
    def number(self, face_id):
        return self.faces[face_id - 1]
 
    def issame(self, other_dice):
        for i in range(4):
            if self.faces[0] == other_dice.faces[0]:
                for j in range(4):
                    if self.faces == other_dice.faces:
                        return True
                    self.rotate()
            self.roll_north()
 
        for i in range(4):
            if self.faces[0] == other_dice.faces[0]:
                for j in range(4):
                    if self.faces == other_dice.faces:
                        return True
                    self.rotate()
            self.roll_east()
        return False
 
dice_list = []
n = int(input())
for i in range(n):
    dice_list.append(Dice([int(a) for a in input().split()]))
 
res = "Yes"
for i in range(n):
    for j in range(i + 1, n):
        if dice_list[i].issame(dice_list[j]):
            res = "No"
            break
print(res)

Solution (Python):
class Dice(object):

    def __init__(self, num):
        self.num = num

    def rotate_S(self):
        self.num = [self.num[4], self.num[0], self.num[2], self.num[3], self.num[5], self.num[1]]

    def rotate_N(self):
        self.num = [self.num[1], self.num[5], self.num[2], self.num[3], self.num[0], self.num[4]]

    def rotate_W(self):
        self.num = [self.num[2], self.num[1], self.num[5], self.num[0], self.num[4], self.num[3]]

    def rotate_E(self):
        self.num = [self.num[3], self.num[1], self.num[0], self.num[5], self.num[4], self.num[2]]

    def rotate_M(self):
        self.num = [self.num[0], self.num[2], self.num[4], self.num[1], self.num[3], self.num[5]]


def is_equal_dice(dice1, dice2):
    if len(set(dice1.num) - set(dice2.num)) != 0:
        return 0
    else:
        num = []
        for i in range(6):
            if dice1.num[0] == dice2.num[i]:
                num.append(i)
        for j in num:
            if j == 1:
                dice2.rotate_N()
            elif j == 2:
                dice2.rotate_W()
            elif j == 3:
                dice2.rotate_E()
            elif j == 4:
                dice2.rotate_S()
            elif j == 5:
                dice2.rotate_S()
                dice2.rotate_S()

            if dice1.num[5] == dice2.num[5]:
                for _ in range(4):
                    if (dice1.num[1] == dice2.num[1]) & (dice1.num[2] == dice2.num[2]) & (dice1.num[3] == dice2.num[3]):
                        return 1
                    dice1.rotate_M()
        return 0
N = input()
dice = [Dice(map(int, raw_input().split())) for _ in range(N)]
flg = False
for i in range(N):
    for j in range(i+1, N):
        if is_equal_dice(dice[i], dice[j]):
            flg = True
            break
if flg:
    print 'No'
else:
    print 'Yes'

Solution (Python):
import copy

class Dice(object):
	def __init__(self,List):
		self.face=List
		
	def n_spin(self,a_list):
		List=copy.copy(a_list)
		temp=List[0]
		List[0]=List[1]
		List[1]=List[5]
		List[5]=List[4]
		List[4]=temp
		return List
		
 	def s_spin(self,a_list):
 		List=copy.copy(a_list)
 		temp=List[0]
 		List[0]=List[4]
 		List[4]=List[5]
		List[5]=List[1]
		List[1]=temp
		return List
		
	def e_spin(self,a_list):
		List=copy.copy(a_list)
		temp=List[0]
		List[0]=List[3]
		List[3]=List[5]
		List[5]=List[2]
		List[2]=temp
		return List
		
 	def w_spin(self,a_list):
 		List = copy.copy(a_list)
 		temp=List[0]
 		List[0]=List[2]
 		List[2]=List[5]
		List[5]=List[3]
		List[3]=temp
		return List

n=input()
dice_List=[None]*n
emer=[None]*n
for a in range(n):
	dice_List[a]=Dice(map(int,raw_input().split()))
	emer[a]=copy.copy(dice_List[a].face)
judge=True
for b in range(1,n):
	face_storage=[]
	face_storage.append(emer[b])
	for c in range(24):
		face_storage.append((dice_List[b].n_spin(face_storage[c])))
		face_storage.append((dice_List[b].e_spin(face_storage[c])))
		face_storage.append((dice_List[b].w_spin(face_storage[c])))
		face_storage.append((dice_List[b].s_spin(face_storage[c])))
	for d in face_storage:
		if d==dice_List[0].face:
			judge=False
			break
if judge:
	print("Yes")
else :
	print("No")

Solution (Python):
class Dice:
    same_dice_index = ((0, 1, 2, 3, 4, 5), (0, 2, 4, 1, 3, 5), (0, 3, 1, 4, 2, 5),
                       (0, 4, 3, 2, 1, 5), (1, 0, 3, 2, 5, 4), (1, 2, 0, 5, 3, 4),
                       (1, 3, 5, 0, 2, 4), (1, 5, 2, 3, 0, 4), (2, 0, 1, 4, 5, 3),
                       (2, 1, 5, 0, 4, 3), (2, 4, 0, 5, 1, 3), (2, 5, 4, 1, 0, 3),
                       (3, 0, 4, 1, 5, 2), (3, 1, 0, 5, 4, 2), (3, 4, 5, 0, 1, 2),
                       (3, 5, 1, 4, 0, 2), (4, 0, 2, 3, 5, 1), (4, 2, 5, 0, 3, 1),
                       (4, 3, 0, 5, 2, 1), (4, 5, 3, 2, 0, 1), (5, 1, 3, 2, 4, 0),
                       (5, 2, 1, 4, 3, 0), (5, 3, 4, 1, 2, 0), (5, 4, 2, 3, 1, 0))

    def __init__(self, str_list):
        self.faces = str_list

    def is_same(self, dice):
        for i in Dice.same_dice_index:
            for n, j in enumerate(i):
                if dice.faces[n] != self.faces[j]:
                    break
            else:
                return True
        return False

    def is_unique(self, dices_list):
        for i in dices_list:
            if self.is_same(i):
                return False
        return True

import sys

n = int(sys.stdin.readline())

dices = []

for i in range(n):
    str_list = sys.stdin.readline().split()
    new_dice = Dice(str_list)
    if not new_dice.is_unique(dices):
        print('No')
        break
    dices.append(new_dice)
else:
    print('Yes')

Solution (Python):
class Dice:
    same_dice_index = ((0, 1, 2, 3, 4, 5), (0, 2, 4, 1, 3, 5), (0, 3, 1, 4, 2, 5),
                       (0, 4, 3, 2, 1, 5), (1, 0, 3, 2, 5, 4), (1, 2, 0, 5, 3, 4),
                       (1, 3, 5, 0, 2, 4), (1, 5, 2, 3, 0, 4), (2, 0, 1, 4, 5, 3),
                       (2, 1, 5, 0, 4, 3), (2, 4, 0, 5, 1, 3), (2, 5, 4, 1, 0, 3),
                       (3, 0, 4, 1, 5, 2), (3, 1, 0, 5, 4, 2), (3, 4, 5, 0, 1, 2),
                       (3, 5, 1, 4, 0, 2), (4, 0, 2, 3, 5, 1), (4, 2, 5, 0, 3, 1),
                       (4, 3, 0, 5, 2, 1), (4, 5, 3, 2, 0, 1), (5, 1, 3, 2, 4, 0),
                       (5, 2, 1, 4, 3, 0), (5, 3, 4, 1, 2, 0), (5, 4, 2, 3, 1, 0))

    def __init__(self, str_list):
        self.faces = str_list

    def is_same(self, dice):
        for i in Dice.same_dice_index:
            for n, j in enumerate(i):
                if dice.faces[n] != self.faces[j]:
                    break
            else:
                return True
        return False

    def is_unique(self, dices_list):
        for i in dices_list:
            if self.is_same(i):
                return False
        return True

import sys

n = int(sys.stdin.readline())

dices = []

for i in range(n):
    str_list = sys.stdin.readline().split()
    new_dice = Dice(str_list)
    if not new_dice.is_unique(dices):
        print('No')
        break
    dices.append(new_dice)
else:
    print('Yes')

Solution (Python):
class Dice:
    same_dice_index = ((0, 1, 2, 3, 4, 5), (0, 2, 4, 1, 3, 5), (0, 3, 1, 4, 2, 5),
                       (0, 4, 3, 2, 1, 5), (1, 0, 3, 2, 5, 4), (1, 2, 0, 5, 3, 4),
                       (1, 3, 5, 0, 2, 4), (1, 5, 2, 3, 0, 4), (2, 0, 1, 4, 5, 3),
                       (2, 1, 5, 0, 4, 3), (2, 4, 0, 5, 1, 3), (2, 5, 4, 1, 0, 3),
                       (3, 0, 4, 1, 5, 2), (3, 1, 0, 5, 4, 2), (3, 4, 5, 0, 1, 2),
                       (3, 5, 1, 4, 0, 2), (4, 0, 2, 3, 5, 1), (4, 2, 5, 0, 3, 1),
                       (4, 3, 0, 5, 2, 1), (4, 5, 3, 2, 0, 1), (5, 1, 3, 2, 4, 0),
                       (5, 2, 1, 4, 3, 0), (5, 3, 4, 1, 2, 0), (5, 4, 2, 3, 1, 0))

    def __init__(self, str_list):
        self.faces = str_list

    def is_same(self, dice):
        for i in Dice.same_dice_index:
            for n, j in enumerate(i):
                if dice.faces[n] != self.faces[j]:
                    break
            else:
                return True
        return False

    def is_unique(self, dices_list):
        for i in dices_list:
            if self.is_same(i):
                return False
        return True

import sys

n = int(sys.stdin.readline())

dices = []

for i in range(n):
    str_list = sys.stdin.readline().split()
    new_dice = Dice(str_list)
    if not new_dice.is_unique(dices):
        print('No')
        break
    dices.append(new_dice)
else:
    print('Yes')

Solution (Python):

import sys
n = input()
rolls =[]
for x in xrange(n):
    rolls.append(raw_input().split())
def pareExist(li1,li2):
    for x in xrange(3):
        if set(rolls1pare[x]) not in map(set,rolls2pare):
            return False
    return True


for l1 in xrange(n):
    for l2 in xrange(n):
        
        
        if l1==l2:
            break
        rolls_copy = rolls
        
        rolls1pare = [(rolls_copy[l1][0],rolls_copy[l1][5]),(rolls_copy[l1][2],rolls_copy[l1][3]),(rolls_copy[l1][1],rolls_copy[l1][4])]
        testpare = (rolls_copy[l1][0],rolls_copy[l1][5])
        testparerev = (rolls_copy[l1][5],rolls_copy[l1][0])
        rolls2pare = [(rolls_copy[l2][0],rolls_copy[l2][5]),(rolls_copy[l2][2],rolls_copy[l2][3]),(rolls_copy[l2][1],rolls_copy[l2][4])]
        
        
        
        if not pareExist(rolls1pare,rolls2pare):
            break
        
        if testpare in rolls2pare:
            pareps = rolls2pare.index(testpare)
            revflag = False
        elif testparerev in rolls2pare:
            pareps = rolls2pare.index(testparerev)
            revflag = True
        else:
            break
        
        
        li1 = [rolls_copy[l1][1],rolls_copy[l1][2],rolls_copy[l1][4],rolls_copy[l1][3]]
        
        if pareps==0:
            if revflag:
                tmp = rolls_copy[l2][2]
                rolls_copy[l2][2] = rolls_copy[l2][3]
                rolls_copy[l2][3] = tmp
            li2 = [rolls_copy[l2][1],rolls_copy[l2][2],rolls_copy[l2][4],rolls_copy[l2][3]]
        elif pareps==1:
            if revflag:
                tmp = rolls_copy[l2][1]
                rolls_copy[l2][1] = rolls_copy[l2][4]
                rolls_copy[l2][4] = tmp
            li2 = [rolls_copy[l2][0],rolls_copy[l2][1],rolls_copy[l2][5],rolls_copy[l2][4]]
        elif pareps==2:
            if revflag:
                tmp = rolls_copy[l2][0]
                rolls_copy[l2][0] = rolls_copy[l2][5]
                rolls_copy[l2][5] = tmp
            li2 = [rolls_copy[l2][0],rolls_copy[l2][3],rolls_copy[l2][5],rolls_copy[l2][2]]
        else:
            break
        
        
        
        i=0
        j=li2.index(li1[0])
        matchflag = True
        for x in xrange(4):
            
            if li1[i]==li2[j]:
                j+=1
                i+=1
            else:
                matchflag =False
                break
            if i>3:
                i=0
            if j>3:
                j=0
        if matchflag:
            print "No"
            sys.exit(0)
print "Yes"

Solution (Python):
ROLL_MASTER = (
    (0, 1, 2, 3, 4, 5), (1, 0, 3, 2, 5, 4), (2, 0, 1, 4, 5, 3),
    (3, 0, 4, 1, 5, 2), (4, 0, 2, 3, 5, 1), (5, 1, 3, 2, 4, 0))
TWIST_MASTER = (0, 2, 4, 1, 3, 5)


def roll(dice, top):
    return tuple(dice[i] for i in ROLL_MASTER[top])


def twist(dice):
    return tuple(dice[i] for i in TWIST_MASTER)


def twist_check(d1, d2):
    if d1[0] != d2[0] or d1[5] != d2[5]:
        return False
    for _ in range(4):
        if d1 == d2:
            return True
        d2 = twist(d2)
    return False


def equal_check(d1, d2):
    for i in range(6):
        if twist_check(d1, roll(d2, i)):
            return True
    return False


def differs_all():
    n, d1, dset = int(input()), tuple(map(int, input().split())), set()
    dset.add(d1)

    for _ in range(n - 1):
        d2 = tuple(map(int, input().split()))
        for d1 in dset:
            if equal_check(d1, d2):
                return False
        dset.add(d2)
    return True


print('Yes' if differs_all() else 'No')

Solution (Python):
class Dice():
    def __init__(self, nums):
        self.nums = nums
        self.top, self.front, self.right = 0, 1, 2

    def __eq__(self, dice):
        for x in "NNNNEEE":
            dice.move(x)
            for _ in range(4):
                dice.move("ESW")
                if self.nums[self.top] == dice.nums[dice.top] and \
                        self.nums[self.front] == dice.nums[dice.front] and \
                        self.nums[self.right] == dice.nums[dice.right] and \
                        self.nums[5 - self.top] == dice.nums[5 - dice.top] and \
                        self.nums[5 - self.front] == dice.nums[5 - dice.front] and \
                        self.nums[5 - self.right] == dice.nums[5 - dice.right]:
                    return True
        return False

    def move(self, op):
        for c in op:
            if c == 'N':
                self.top, self.front = self.front, 5 - self.top
            elif c == 'S':
                self.top, self.front = 5 - self.front, self.top
            elif c == 'E':
                self.top, self.right = 5 - self.right, self.top
            else:
                self.top, self.right = self.right, 5 - self.top

n = int(input())
ans = "Yes"
dices = []
dices.append(Dice([int(n) for n in input().split()]))
for _ in range(n - 1):
    new_dice = Dice([int(n) for n in input().split()])
    for dice in dices:
        if dice == new_dice:
            ans = "No"
            break
    dices.append(new_dice)
print(ans)

Solution (Python):


class dice_class:
    def __init__(self, list):
        self.num = list

    def sut(self, top):  
        i = 0
        while i < 3:
            if self.num[0] == top:
                break
            else:
                self.roll('E')
            if self.num[0] == top:
                break
            else:
                self.roll('N')
            i += 1

    def suf(self, front):   
        i = 0
        while i < 4:
            if self.num[1] == front:
                break
            else:
                self.roll('SEN')
            i += 1

    def roll(self, s):
        for i in s:
            if i == 'E':
                self.rollE()
            elif i == 'N':
                self.rollN()
            elif i == 'S':
                self.rollS()
            elif i == 'W':
                self.rollW()

    def rollE(self):
        self.num = [self.num[3], self.num[1], self.num[0], self.num[5], self.num[4], self.num[2]]

    def rollN(self):
        self.num = [self.num[1], self.num[5], self.num[2], self.num[3], self.num[0], self.num[4]]

    def rollS(self):
        self.num = [self.num[4], self.num[0], self.num[2], self.num[3], self.num[5], self.num[1]]

    def rollW(self):
        self.num = [self.num[2], self.num[1], self.num[5], self.num[0], self.num[4], self.num[3]]


if __name__ == "__main__":
    n = int(raw_input())
    dice_List = []
    for i in range(n):
        dice_List.append(dice_class(map(int, raw_input().split())))
    flg = True

    for i in range(n):
        for j in range(i+1, n):
            for k in range(6):
                if k % 2:
                    dice_List[i].roll('E')
                else:
                    dice_List[i].roll('N')
                dice_List[j].sut(dice_List[i].num[0])
                dice_List[j].suf(dice_List[i].num[1])
                if dice_List[i].num == dice_List[j].num:
                    flg = False
                    break
    if flg:
        print "Yes"
    else:
        print "No"

Solution (Python):
import sys
import itertools

class Dice:
    def __init__(self):
        self.front = '1'
        self.back = '6'
        self.top = '5'
        self.bottom = '2'
        self. right = '3'
        self.left = '4'
    def turnRight(self):
        temp = self.top
        self.top = self.left
        self.left = self.bottom
        self.bottom = self.right
        self.right = temp
        return self.top
    def turnLeft(self):
        temp = self.top
        self.top = self.right
        self.right = self.bottom
        self.bottom = self.left
        self.left = temp
        return self.top
    def turnFront(self):
        temp = self.top
        self.top = self.back
        self.back = self.bottom
        self.bottom = self.front
        self.front = temp
        return self.top
    def turnBack(self):
        temp = self.top
        self.top = self.front
        self.front = self.bottom
        self.bottom = self.back
        self.back = temp
        return self.top
    def __eq__(self, other):
        return (self.top, self.bottom, self.front, self.back, self.right, self.left) == other
    def __ne__(self, other):
        return (self.top, self.bottom, self.front, self.back, self.right, self.left) != other

def compdice(A, B):
    for rot1 in range(4):
        for rot2 in range(4):
            if A == B:
                return True
            B.turnRight()
        B.turnFront()
    B.turnRight()
    B.turnFront()
    for rot in range(4):
        if A == B:
            return True
        B.turnRight()
    B.turnBack()
    B.turnBack()
    for rot in range(4):
        if A == B:
            return True
        B.turnRight()
    return False

ans = 'Yes'
n = int(input())
dices = []
for line in range(n):
    dice = Dice()
    dice.top, dice.front, dice.right, dice.left, dice.back, dice.bottom = input().split()
    dices.append(dice)
dicepairs = itertools.combinations(dices, 2)
for dicepair in dicepairs:
    A, B = dicepair
    if compdice(A, B):
        ans = 'No'
print(ans)

Solution (Python):
def move_dice(d, dir):
    if(dir == 'N'):
        d[0], d[1], d[4], d[5] = d[1], d[5], d[0], d[4]
    if(dir == 'S'):
        d[0], d[1], d[4], d[5] = d[4], d[0], d[5], d[1]
    if(dir == 'E'):
        d[0], d[2], d[3], d[5] = d[3], d[0], d[5], d[2]
    if(dir == 'W'):
        d[0], d[2], d[3], d[5] = d[2], d[5], d[0], d[3]

def rotate_right_dice(d):
    d[1], d[2], d[3], d[4] = d[3], d[1], d[4], d[2]

def to_up_dice(d, num_index):
    move_num = {0:0, 1:3, 2:2, 3:0, 4:1, 5:2}
    if(num_index==2 or num_index==3):
        move_dice(d, 'E')
    for _ in range(move_num[num_index]):
        move_dice(d, 'N')

def to_front_dice(d, num):
    while(d[1] != num):
        rotate_right_dice(d)

def equal_dice(d1, d2):
    for i in range(6):
        tmp_d = list(d1)
        to_up_dice(tmp_d, i)
        for _ in range(4):
            if(tmp_d == d2): return 'Yes'
            else: rotate_right_dice(tmp_d)
    return 'No'

def equal_dice_n(d_list, n):
    for i in range(n-1):
        for j in range(i+1, n):
            if(equal_dice(d_list[i], d_list[j]) == 'Yes'):
                return 'No'
    return 'Yes'

n = int(input())
d_list = [list(map(int, input().split())) for _ in range(n)]
print(equal_dice_n(d_list, n))

Solution (Python):
n=int(input())

def rot(d, r):
    if r == 'N':
        return (d[1], d[5], d[2], d[3], d[0], d[4])
    if r == 'E':
        return (d[3], d[1], d[0], d[5], d[4], d[2])
    if r == 'W':
        return (d[2], d[1], d[5], d[0], d[4], d[3])
    if r == 'S':
        return (d[4], d[0], d[2], d[3], d[5], d[1])
    return d

def gen_all( d1 ):
    ds = { d1 }
    while 1:
        ds2 = { rot(d, r) for d in ds for r in list('NEWS') }.union(ds)
        if ds == ds2: break
        ds = ds2
    return ds

ds = {}

for i in range(n):
    d=tuple(map(int,input().split()))
    ds[i] = (d, gen_all(d))

dep=any ([ ds[i][0] in ds[j][1] for i in range(n) for j in range(i+1,n) ])
    
print('No' if dep else 'Yes')

Solution (Python):

def s2n(dice):
    aft_dice = [dice[1],dice[5],dice[2],dice[3],dice[0],dice[4]]
    return aft_dice


def w2e(dice):
    aft_dice = [dice[3],dice[1],dice[0],dice[5],dice[4],dice[2]]
    return aft_dice


def rspin(dice):
    aft_dice = [dice[0],dice[2],dice[4],dice[1],dice[3],dice[5]]
    return aft_dice


def isd(dice1, dice2):
    flag = 0
    for i in xrange(4):
        for j in xrange(4):
            flag += 1 if dice1 == dice2 else 0
            dice2 = rspin(dice2)
        dice2 = s2n(dice2)

    dice2 = w2e(dice2)
    for i in xrange(4):
        flag += 1 if dice1 == dice2 else 0
        dice2 = rspin(dice2)

    dice2 = w2e(dice2)
    dice2 = w2e(dice2)
    for i in xrange(4):
        flag += 1 if dice1 == dice2 else 0
        dice2 = rspin(dice2)
    return flag


dices = []
n = input()
for i in xrange(n):
    dices.append( map(int, raw_input().split()) )

for i in xrange(n-1):
    for j in xrange(i+1,n):
        if isd(dices[i], dices[j]) > 0:
            print "No"
            exit()
print "Yes"

Solution (Python):
import random
class Dice:
    def __init__(self, ls):
        self.top,   self.bottom = ls[0], ls[5]
        self.right, self.left   = ls[2], ls[3]
        self.front, self.back   = ls[1], ls[4]
    def rot(self, d):
        if   d == "N":
            (self.top, self.back, self.bottom, self.front) = (
             self.front, self.top, self.back, self.bottom)
        elif d == "S":
            (self.top, self.back, self.bottom, self.front) = (
             self.back, self.bottom, self.front, self.top)
        elif d == "E":
            (self.top, self.right, self.bottom, self.left) = (
             self.left, self.top, self.right, self.bottom)
        elif d == "W":
            (self.top, self.right, self.bottom, self.left) = (
             self.right, self.bottom, self.left, self.top)
    def show(self):
        return (self.top, self.bottom, self.right,
                self.left, self.front, self.back)
S = set()
N = int(raw_input())
judge = True
for i in xrange(N):
    dice = Dice(map(int, raw_input().split()))
    s = set()
    for loop in xrange(1000):
        nums = dice.show()
        if nums in S:
            judge = False
            break
        s.add(nums)
        dice.rot("NSEW"[random.randint(0, 3)])
    if not judge:
        break
    S |= s
print "Yes" if judge else "No"

Solution (Python):
class dice:
    def __init__(self,label):
        self.label = {i+1: l for i,l in enumerate(label)}
    def roll(self,op):
        l = self.label
        if   op=='N': self.label = {1:l[2], 2:l[6], 3:l[3], 4:l[4], 5:l[1], 6:l[5]}
        elif op=='E': self.label = {1:l[4], 2:l[2], 3:l[1], 4:l[6], 5:l[5], 6:l[3]}
        elif op=='W': self.label = {1:l[3], 2:l[2], 3:l[6], 4:l[1], 5:l[5], 6:l[4]}
        elif op=='S': self.label = {1:l[5], 2:l[1], 3:l[3], 4:l[4], 5:l[6], 6:l[2]}
    def yaw(self,op):
        l = self.label
        if   op=='CW' : self.label = {1:l[1], 2:l[3], 3:l[5], 4:l[2], 5:l[4], 6:l[6]}
        elif op=='CCW': self.label = {1:l[1], 2:l[4], 3:l[2], 4:l[5], 5:l[3], 6:l[6]}
    def get_label(self,i=0):
        if 1 <= i <= 6:
            return self.label[i]
        else:
            return [self.label[x] for x in range(1,7)] 

def cmp_dice(d0,d1):
    dc = dice(d0.get_label())
    d1_l = d1.get_label()
    result=False
    if set(dc.get_label())==set(d1_l):
        for i in range(6):
            for _ in range(4):
                if dc.get_label()==d1_l:
                    result=True
                    break
                dc.yaw('CW')
            if dc.get_label()==d1_l:
                break
            if i < 4:
                dc.roll('E')
            if i >= 3:
                dc.roll('N')
            if i >= 4:
                dc.roll('N')
    return result

if __name__ == '__main__':
    import itertools
    n=int(input())
    d_list = [dice(list(map(int, input().split()))) for _ in range(n)]
    result='Yes'
    for dx, dy in itertools.combinations(d_list,2):
        if cmp_dice(dx,dy):
            result='No'
            break
    print(result)

Solution (Python):
class dice:
    def __init__(self,label):
        self.label = {i+1: l for i,l in enumerate(label)}
    def roll(self,op):
        l = self.label
        if   op=='N': self.label = {1:l[2], 2:l[6], 3:l[3], 4:l[4], 5:l[1], 6:l[5]}
        elif op=='E': self.label = {1:l[4], 2:l[2], 3:l[1], 4:l[6], 5:l[5], 6:l[3]}
        elif op=='W': self.label = {1:l[3], 2:l[2], 3:l[6], 4:l[1], 5:l[5], 6:l[4]}
        elif op=='S': self.label = {1:l[5], 2:l[1], 3:l[3], 4:l[4], 5:l[6], 6:l[2]}
    def yaw(self,op):
        l = self.label
        if   op=='CW' : self.label = {1:l[1], 2:l[3], 3:l[5], 4:l[2], 5:l[4], 6:l[6]}
        elif op=='CCW': self.label = {1:l[1], 2:l[4], 3:l[2], 4:l[5], 5:l[3], 6:l[6]}
    def get_label(self,i=0):
        if 1 <= i <= 6:
            return self.label[i]
        else:
            return [self.label[x] for x in range(1,7)] 

def cmp_dice(d0,d1):
    dc = dice(d0.get_label())
    d1_l = d1.get_label()
    result=False
    if set(dc.get_label())==set(d1_l):
        for i in range(6):
            for _ in range(4):
                if dc.get_label()==d1_l:
                    result=True
                    break
                dc.yaw('CW')
            if dc.get_label()==d1_l:
                break
            if i < 4:
                dc.roll('E')
            if i >= 3:
                dc.roll('N')
            if i >= 4:
                dc.roll('N')
    return result

if __name__ == '__main__':
    import itertools
    d_grp={}
    for _ in range(int(input())):
        d = dice(list(map(int, input().split())))
        d_set = tuple(set(d.get_label()))
        if d_set in d_grp:
            d_grp[d_set].append(d)
        else:
            d_grp[d_set]=[d]
    result='Yes'
    for d_list in d_grp.values():
        for dx, dy in itertools.combinations(d_list,2):
            if cmp_dice(dx,dy):
                result='No'
                break
    print(result)

Solution (Python):
import sys
import math

def str_input():
    S = raw_input()
    if S[len(S)-1] == "\r":
        return S[:len(S)-1]
    return S

def float_to_str(num):
    return str("{:.10f}".format(num))

def list_input(tp):
    return map(tp, str_input().split())



class Dice:
    pip = [0 for i in xrange(6)]

    def __init__(self, arg):
        self.pip = arg

    def rollDir(self, dr):
        nextPip = [0 for i in xrange(6)]
        if dr == "N":
            nextPip[0] = self.pip[1]
            nextPip[1] = self.pip[5]
            nextPip[2] = self.pip[2]
            nextPip[3] = self.pip[3]
            nextPip[4] = self.pip[0]
            nextPip[5] = self.pip[4]
        elif dr == "E":
            nextPip[0] = self.pip[3]
            nextPip[1] = self.pip[1]
            nextPip[2] = self.pip[0]
            nextPip[3] = self.pip[5]
            nextPip[4] = self.pip[4]
            nextPip[5] = self.pip[2]
        self.pip = nextPip

    def roll(self, dr):
        if dr == "N" or dr == "E":
            self.rollDir(dr)
        elif dr == "S":
            self.rollDir("N")
            self.rollDir("N")
            self.rollDir("N")
        elif dr == "W":
            self.rollDir("E")
            self.rollDir("E")
            self.rollDir("E")

    def check(self, a, b):
        while self.pip[0] != a:
            if self.pip[2] != a and self.pip[3] != a:
                self.roll("N")
            else:
                self.roll("N")
                self.roll("W")
                self.roll("S")

        while self.pip[1] != b:
            self.roll("N")
            self.roll("W")
            self.roll("S")
        return self.pip[2]

def sameCheck(dice1, dice2):
    for i in xrange(4):
        for j in xrange(4):
            if dice1.pip == dice2.pip:
                return True
            dice2.roll("E")
        dice2.roll("N")
    dice2.roll("N")
    dice2.roll("E")
    dice2.roll("S")
    for i in xrange(4):
        if dice1.pip == dice2.pip:
            return True
        dice2.roll("E")
    dice2.roll("N")
    dice2.roll("E")
    dice2.roll("E")
    dice2.roll("S")
    for i in xrange(4):
        if dice1.pip == dice2.pip:
            return True
        dice2.roll("E")
    return False

n = input()
dice = []

for i in xrange(n):
    tmp_dice = Dice(list_input(int))
    dice.append(tmp_dice)

for i in xrange(n):
    for j in xrange(i+1, n):
        if sameCheck(dice[i], dice[j]):
            print "No"
            sys.exit()

print "Yes"

Solution (Python):
class dice:
    def __init__(self,label):
        self.top, self.front, self.right, self.left, self.rear, self.bottom \
                = label
 
    def roll(self,op):
        if   op=='N': self.top, self.front, self.bottom, self.rear = \
                                self.front, self.bottom, self.rear , self.top
        elif op=='E': self.top, self.left , self.bottom, self.right = \
                                self.left , self.bottom, self.right, self.top
        elif op=='W': self.top, self.right, self.bottom, self.left = \
                                self.right, self.bottom, self.left , self.top
        elif op=='S': self.top, self.rear , self.bottom, self.front = \
                                self.rear , self.bottom, self.front, self.top
 
    def print_top(self): print(self.top)
    def print_right(self): print(self.right)
 
    def get_label(self):
        return [self.top, self.front, self.right, self.left, self.rear, self.bottom]

    def compare(self, label):
        for op in 'EEENEEENEEESEEESEEENEEEN':
            if self.get_label() == label:
                return True
            self.roll(op)
        else:
            return False
  
if __name__=='__main__':
    n = int(input())
    L = []
    for i in range(n):
        label = list(map(int,input().split()))
        for d in L:
            if d.compare(label): break
        else:
            L.append(dice(label))
            continue
        print('No')
        break
    else:
        print('Yes')