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, nums ):
		self.face = nums
	
	def rolltoTopIndex( self, faceindex ):	
		if faceindex == 1:
			self.roll( "N" )
		elif faceindex == 2:
			self.roll( "W" )
		elif faceindex == 3:
			self.roll( "E" )
		elif faceindex == 4:
			self.roll( "S" )
		elif faceindex == 5:
			self.roll( "NN" )
	
	def roll( self, actions ):
		for act in actions:
			t = 0
			if act == "E":
				t =   self.face[0]
				self.face[0] =   self.face[3]
				self.face[3] =   self.face[5]
				self.face[5] =   self.face[2]
				self.face[2] = t
			elif  act == "N":
				t =   self.face[0]
				self.face[0] =   self.face[1]
				self.face[1] =   self.face[5]
				self.face[5] =   self.face[4]
				self.face[4] = t
			elif  act == "S":
				t =  self.face[ 0]
				self.face[0] =  self.face[4]
				self.face[4] =  self.face[5]
				self.face[5] =  self.face[1]
				self.face[1] = t	
			elif  act == "W":
				t =  self.face[0]
				self.face[0] =  self.face[2]
				self.face[2] =  self.face[5]
				self.face[5] =  self.face[3]
				self.face[3] = t	
			elif  act == "M":
				t =  self.face[1]
				self.face[1] =  self.face[2]
				self.face[2] =  self.face[4]
				self.face[4] =  self.face[3]
				self.face[3] = t

def isDiffDice( dice1, dice2 ):
	diceface = dice2.face
	cnt = 0
	for i in range( 6 ):
		if dice1.face[0] == dice2.face[i]:
			dice2.rolltoTopIndex( i )
			for j in range( 1, 5 ):
				if dice1.face[1] == dice2.face[1]:
					cnt = 0
					for k in range( 6 ):
						if dice1.face[k] == dice2.face[k]:
							cnt += 1
					if cnt == 6:
						break
				dice2.roll( "M" )
		if cnt == 6:
			break
		dice2.face = diceface
			
	if cnt == 6:
		return False
	else:
		return True


n = int( raw_input( ) )
dices = [ Dice( [ int( val ) for val in raw_input( ).split( " " ) ] ) for i in range( n ) ]

isdiff = True
for i in range( 0, n-1 ):
	for j in range( i+1, n ):
		if not isDiffDice( dices[i], dices[j] ):
			print( "No" )
			isdiff = False
			break
	if not isdiff:
		break

if isdiff:
	print( "Yes" )

Solution (Python):
class Dice:	
	def __init__( self, nums ):
		self.face = nums
	
	def rolltoTopIndex( self, faceindex ):	
		if faceindex == 1:
			self.roll( "N" )
		elif faceindex == 2:
			self.roll( "W" )
		elif faceindex == 3:
			self.roll( "E" )
		elif faceindex == 4:
			self.roll( "S" )
		elif faceindex == 5:
			self.roll( "NN" )
	
	def roll( self, actions ):
		for act in actions:
			t = 0
			if act == "E":
				t =   self.face[0]
				self.face[0] =   self.face[3]
				self.face[3] =   self.face[5]
				self.face[5] =   self.face[2]
				self.face[2] = t
			elif  act == "N":
				t =   self.face[0]
				self.face[0] =   self.face[1]
				self.face[1] =   self.face[5]
				self.face[5] =   self.face[4]
				self.face[4] = t
			elif  act == "S":
				t =  self.face[ 0]
				self.face[0] =  self.face[4]
				self.face[4] =  self.face[5]
				self.face[5] =  self.face[1]
				self.face[1] = t	
			elif  act == "W":
				t =  self.face[0]
				self.face[0] =  self.face[2]
				self.face[2] =  self.face[5]
				self.face[5] =  self.face[3]
				self.face[3] = t	
			elif  act == "M":
				t =  self.face[1]
				self.face[1] =  self.face[2]
				self.face[2] =  self.face[4]
				self.face[4] =  self.face[3]
				self.face[3] = t

def isDiffDice( dice1, dice2 ):
	diceface = dice2.face
	cnt = 0
	for i in range( 6 ):
		if dice1.face[0] == dice2.face[i]:
			dice2.rolltoTopIndex( i )
			for j in range( 1, 5 ):
				if dice1.face[1] == dice2.face[1]:
					cnt = 0
					for k in range( 6 ):
						if dice1.face[k] == dice2.face[k]:
							cnt += 1
					if cnt == 6:
						break
				dice2.roll( "M" )
		if cnt == 6:
			break
		dice2.face = diceface
			
	if cnt == 6:
		return False
	else:
		return True


n = int( input( ) )
dices = [ Dice( [ int( val ) for val in input( ).split( " " ) ] ) for i in range( n ) ]

isdiff = True
for i in range( 0, n-1 ):
	for j in range( i+1, n ):
		if not isDiffDice( dices[i], dices[j] ):
			print( "No" )
			isdiff = False
			break
	if not isdiff:
		break

if isdiff:
	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, ary):
        self.top, self.south, self.east, self.west, self.north, self.bottom = ary

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

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

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

    def isEqual(self, dice):
        if self.top == dice.top and self.south == dice.south and self.east == dice.east and self.west == dice.west and self.north == dice.north and self.bottom == dice.bottom:
            return True
        else:
            return False

def isSame(diceX,diceY):
    flag = False
    
    for i in range(6):
        if i % 2 == 0:
            diceX.roll_n()
        else:
            diceX.roll_e()
    
        for j in range(4):
            if diceX.isEqual(diceY):
                flag = True
                break
            else:
                diceX.twist()
    
        if flag:
            break
    
    if flag:
        return True
    else:
        return False

n = int(input())
dices = [Dice(map(int,input().split())) for i in range(n)]

flag = False
for i in range(len(dices)):
    for j in range(i+1, len(dices)):
        if isSame(dices[i],dices[j]):
            flag = True

if not flag:
    print('Yes')
else:
    print('No')

Solution (Python):

from itertools import combinations


class Dice(object):

    def __init__(self, list_):
        self.label = list_

    def getLabel(self, i):
        return self.label[i - 1]

    def _rotateS(self):
        self.label = [self.getLabel(i) for i in [5, 1, 3, 4, 6, 2]]

    def _rotateN(self):
        self.label = [self.getLabel(i) for i in [2, 6, 3, 4, 1, 5]]

    def _rotateE(self):
        self.label = [self.getLabel(i) for i in [4, 2, 1, 6, 5, 3]]

    def _rotateW(self):
        self.label = [self.getLabel(i) for i in [3, 2, 6, 1, 5, 4]]

    def _spinPos(self):
        self.label = [self.getLabel(i) for i in [1, 4, 2, 5, 3, 6]]

    def _spinNeg(self):
        self.label = [self.getLabel(i) for i in [1, 3, 5, 2, 4, 6]]

    def rotate(self, rotates):
        for r in rotates:
            if r == 'S':
                self._rotateS()
            elif r == 'N':
                self._rotateN()
            elif r == 'E':
                self._rotateE()
            elif r == 'W':
                self._rotateW()
            elif r == 'T':
                self._spinPos()
            elif r == 'B':
                self._spinNeg()

    def matchTopFront(self, top, front):
        try:
            iTop = self.label.index(top) + 1
            topRot = {1: '', 2: 'N', 3: 'W', 4: 'E', 5: 'S', 6: 'SS'}
            self.rotate(topRot[iTop])

            iFront = self.label.index(front) + 1
            frontRot = {2: '', 3: 'B', 4: 'T', 5: 'TT'}
            self.rotate(frontRot[iFront])
            return True
        except KeyError:
            return False

    def copy(self):
        return Dice(self.label[:])

    def __eq__(self, other):
        
        for rs in ['', 'S', 'N', 'E', 'W', 'SS']:
            tmp = self.copy()
            for r in rs:
                tmp.rotate(r)
            
            top = tmp.getLabel(1) == other.getLabel(1)
            bottom = tmp.getLabel(6) == other.getLabel(6)
            
            

            def _side():
                result, n = False, 3
                while not result and n > 0:
                    if tmp.label[1:5] == other.label[1:5]:
                        result = True
                    else:
                        tmp.rotate('T')
                        n -= 1
                return result

            if top and bottom and _side():
                return True
        else:
            return False


def sub(face1, face2):
    
    if set(face1) != set(face2):
        return False
    
    elif len(face1) == len(set(face1)):
        d1 = Dice(face1)
        d2 = Dice(face2)
        if d2.matchTopFront(d1.getLabel(1), d1.getLabel(2)):
            return True if d1.label == d2.label else False
        else:
            return False
    
    else:
        d1 = Dice(face1)
        d2 = Dice(face2)
        return True if d1 == d2 else False


def main():
    n = input()
    faces = [map(int, raw_input().split()) for _ in xrange(n)]
    for f1, f2 in combinations(faces, 2):
        if f1 == f2 or sub(f1, f2) == True:
            print 'No'
            break
    else:
        print 'Yes'


if __name__ == '__main__':
    main()

Solution (Python):

from itertools import combinations


class Dice(object):

    def __init__(self, list_):
        self.label = list_

    def getLabel(self, i):
        return self.label[i - 1]

    def _rotateS(self):
        self.label = [self.getLabel(i) for i in [5, 1, 3, 4, 6, 2]]

    def _rotateN(self):
        self.label = [self.getLabel(i) for i in [2, 6, 3, 4, 1, 5]]

    def _rotateE(self):
        self.label = [self.getLabel(i) for i in [4, 2, 1, 6, 5, 3]]

    def _rotateW(self):
        self.label = [self.getLabel(i) for i in [3, 2, 6, 1, 5, 4]]

    def _spinPos(self):
        self.label = [self.getLabel(i) for i in [1, 4, 2, 5, 3, 6]]

    def _spinNeg(self):
        self.label = [self.getLabel(i) for i in [1, 3, 5, 2, 4, 6]]

    def rotate(self, rotates):
        for r in rotates:
            if r == 'S':
                self._rotateS()
            elif r == 'N':
                self._rotateN()
            elif r == 'E':
                self._rotateE()
            elif r == 'W':
                self._rotateW()
            elif r == 'T':
                self._spinPos()
            elif r == 'B':
                self._spinNeg()

    def matchTopFront(self, top, front):
        try:
            iTop = self.label.index(top) + 1
            topRot = {1: '', 2: 'N', 3: 'W', 4: 'E', 5: 'S', 6: 'SS'}
            self.rotate(topRot[iTop])

            iFront = self.label.index(front) + 1
            frontRot = {2: '', 3: 'B', 4: 'T', 5: 'TT'}
            self.rotate(frontRot[iFront])
            return True
        except KeyError:
            return False

    def copy(self):
        return Dice(self.label[:])

    def _equalWithOverlap(self, other):
        
        for rs in ['', 'S', 'N', 'E', 'W', 'SS']:
            tmp = self.copy()
            for r in rs:
                tmp.rotate(r)
            
            top = tmp.getLabel(1) == other.getLabel(1)
            bottom = tmp.getLabel(6) == other.getLabel(6)
            
            

            def _side():
                result, n = False, 3
                while not result and n > 0:
                    if tmp.label[1:5] == other.label[1:5]:
                        result = True
                    else:
                        tmp.rotate('T')
                        n -= 1
                return result

            if top and bottom and _side():
                return True
        else:
            return False

    def __eq__(self, other):
        
        if set(self.label) != set(other.label):
            return False
        
        elif len(self.label) == len(set(self.label)):
                if other.matchTopFront(self.getLabel(1), self.getLabel(2)):
                    return True if self.label == other.label else False
                else:
                    return False
        
        else:
            return True if self._equalWithOverlap(other) else False


def main():
    n = input()
    faces = [map(int, raw_input().split()) for _ in xrange(n)]
    for f1, f2 in combinations(faces, 2):
        if f1 == f2 or Dice(f1) == Dice(f2):
            print 'No'
            break
    else:
        print 'Yes'


if __name__ == '__main__':
    main()

Solution (Python):

from itertools import combinations


class Dice(object):

    def __init__(self, list_):
        self.label = list_

    def getLabel(self, i):
        return self.label[i - 1]

    def _rotateS(self):
        self.label = [self.getLabel(i) for i in [5, 1, 3, 4, 6, 2]]

    def _rotateN(self):
        self.label = [self.getLabel(i) for i in [2, 6, 3, 4, 1, 5]]

    def _rotateE(self):
        self.label = [self.getLabel(i) for i in [4, 2, 1, 6, 5, 3]]

    def _rotateW(self):
        self.label = [self.getLabel(i) for i in [3, 2, 6, 1, 5, 4]]

    def _spinPos(self):
        self.label = [self.getLabel(i) for i in [1, 4, 2, 5, 3, 6]]

    def _spinNeg(self):
        self.label = [self.getLabel(i) for i in [1, 3, 5, 2, 4, 6]]

    def rotate(self, rotates):
        for r in rotates:
            if r == 'S':
                self._rotateS()
            elif r == 'N':
                self._rotateN()
            elif r == 'E':
                self._rotateE()
            elif r == 'W':
                self._rotateW()
            elif r == 'T':
                self._spinPos()
            elif r == 'B':
                self._spinNeg()

    def matchTopFront(self, top, front):
        iTop = self.label.index(top) + 1
        topRot = {1: '', 2: 'N', 3: 'W', 4: 'E', 5: 'S', 6: 'SS'}
        self.rotate(topRot[iTop])

        iFront = self.label.index(front) + 1
        frontRot = {2: '', 3: 'B', 4: 'T', 5: 'TT'}
        
        try:
            self.rotate(frontRot[iFront])
            return True
        except KeyError:
            return False

    def copy(self):
        return Dice(self.label[:])

    def _equalWithoutOverlap(self, other):
        if other.matchTopFront(self.getLabel(1), self.getLabel(2)):
            return True if self.label == other.label else False
        else:
            return False

    def _equalWithOverlap(self, other):
        
        for rs in ['', 'S', 'N', 'E', 'W', 'SS']:
            tmp = self.copy()
            for r in rs:
                tmp.rotate(r)
            
            top = tmp.getLabel(1) == other.getLabel(1)
            bottom = tmp.getLabel(6) == other.getLabel(6)

            
            
            def _side():
                result, n = False, 3
                while not result and n > 0:
                    if tmp.label[1:5] == other.label[1:5]:
                        result = True
                    else:
                        tmp.rotate('T')
                        n -= 1
                return result

            if top and bottom and _side():
                return True
        else:
            return False

    def __eq__(self, other):
        
        if set(self.label) != set(other.label):
            return False
        
        elif len(self.label) == len(set(self.label)):
            return self._equalWithoutOverlap(other)
        
        else:
            return self._equalWithOverlap(other)


def main():
    n = input()
    faces = [map(int, raw_input().split()) for _ in xrange(n)]
    for f1, f2 in combinations(faces, 2):
        if f1 == f2 or Dice(f1) == Dice(f2):
            print 'No'
            break
    else:
        print 'Yes'


if __name__ == '__main__':
    main()

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(list):
    def __init__(self, l):
        super().__init__(l)

    def roll(self, command):
        
        
        
        if command == 'N':
            self[0], self[1], self[5], self[4] = self[1], self[5], self[4], self[0]
        elif command == 'S':
            self[0], self[1], self[5], self[4] = self[4], self[0], self[1], self[5]
        elif command == 'W':
            self[0], self[3], self[5], self[2] = self[2], self[0], self[3], self[5]
        elif command == 'E':
            self[0], self[3], self[5], self[2] = self[3], self[5], self[2], self[0]

    def right_spin(self, repeat=1):
        for _ in range(repeat):
            self[3], self[1], self[2], self[4] = self[1], self[2], self[4], self[3]

    def _fix_position(self):
        self._min_num_to_top()
        self._min_side_num_to_front()

    def _min_num_to_top(self):
        min_idx = self.index(min(self))
        if min_idx == 0:
            pass
        elif min_idx == 1:
            self.roll('N')
        elif min_idx == 2:
            self.roll('W')
        elif min_idx == 3:
            self.roll('E')
        elif min_idx == 4:
            self.roll('S')
        elif min_idx == 5:
            self.roll('N')
            self.roll('N')

    def _min_side_num_to_front(self):
        idx = self.index(min(self[3], self[1], self[2], self[4]))
        if idx == 1:
            pass
        elif idx == 2:
            self.right_spin(1)
        elif idx == 3:
            self.right_spin(3)
        elif idx == 4:
            self.right_spin(2)

    def __hash__(self):
        self._fix_position()
        return tuple(self).__hash__()


if __name__ == '__main__':
    n = int(input())
    dices = set(Dice(map(int, input().split())) for _ in range(n))
    print('Yes' if n == len(dices) else 'No')

Solution (Python):
class Dice(list):
    def __init__(self, *args):
        super().__init__(*args)
        tmp = self[::]
        self._fix_position()
        self.hash = hash(tuple(self))
        self[::] = tmp

    def roll(self, command):
        if command == 'N':
            self[0], self[1], self[5], self[4] = self[1], self[5], self[4], self[0]
        elif command == 'S':
            self[0], self[1], self[5], self[4] = self[4], self[0], self[1], self[5]
        elif command == 'W':
            self[0], self[3], self[5], self[2] = self[2], self[0], self[3], self[5]
        elif command == 'E':
            self[0], self[3], self[5], self[2] = self[3], self[5], self[2], self[0]

    def spin(self, repeat=1):
        for _ in range(repeat):
            self[3], self[1], self[2], self[4] = self[1], self[2], self[4], self[3]

    def _fix_position(self):
        self._min_num_to_top()
        self._min_side_num_to_front()

    def _min_num_to_top(self):
        idx = self.index(min(self))
        if idx == 0:
            pass
        elif idx == 1:
            self.roll('N')
        elif idx == 2:
            self.roll('W')
        elif idx == 3:
            self.roll('E')
        elif idx == 4:
            self.roll('S')
        elif idx == 5:
            self.roll('N')
            self.roll('N')

    def _min_side_num_to_front(self):
        idx = self.index(min(self[3], self[1], self[2], self[4]))
        if idx == 1:
            pass
        elif idx == 2:
            self.spin(1)
        elif idx == 3:
            self.spin(3)
        elif idx == 4:
            self.spin(2)

    def __hash__(self):
        return self.hash

    def __eq__(self, other):
        return hash(self) == hash(other)


def solve():
    n = int(input())
    dices = set(Dice(map(int, input().split())) for _ in range(n))
    return 'Yes' if n == len(dices) else 'No'


if __name__ == '__main__':
    print(solve())

Solution (Python):
class Dice(list):
    def __init__(self, *args):
        super().__init__(*args)

    def roll(self, command):
        if command == 'N':
            self[0], self[1], self[5], self[4] = self[1], self[5], self[4], self[0]
        elif command == 'S':
            self[0], self[1], self[5], self[4] = self[4], self[0], self[1], self[5]
        elif command == 'W':
            self[0], self[3], self[5], self[2] = self[2], self[0], self[3], self[5]
        elif command == 'E':
            self[0], self[3], self[5], self[2] = self[3], self[5], self[2], self[0]

    def spin(self, repeat=1):
        for _ in range(repeat):
            self[3], self[1], self[2], self[4] = self[1], self[2], self[4], self[3]

    def _fix_position(self):
        self._min_num_to_top()
        self._min_side_num_to_front()

    def _min_num_to_top(self):
        idx = self.index(min(self))
        if idx == 0:
            pass
        elif idx == 1:
            self.roll('N')
        elif idx == 2:
            self.roll('W')
        elif idx == 3:
            self.roll('E')
        elif idx == 4:
            self.roll('S')
        elif idx == 5:
            self.roll('N')
            self.roll('N')

    def _min_side_num_to_front(self):
        idx = self.index(min(self[3], self[1], self[2], self[4]))
        if idx == 1:
            pass
        elif idx == 2:
            self.spin(1)
        elif idx == 3:
            self.spin(3)
        elif idx == 4:
            self.spin(2)

    def __hash__(self):
        self._fix_position()
        return hash(tuple(self))

    def __eq__(self, other):
        return hash(self) == hash(other)


if __name__ == '__main__':
    n = int(input())
    dices = set(Dice(map(int, input().split())) for _ in range(n))
    print('Yes' if n == len(dices) else 'No')

Solution (Python):
import random

class Dice:
  def __init__(self, list = map(str, range(1, 7))):
    self.top = list[0]
    self.front = list[1]
    self.right = list[2]
    self.left = list[3]
    self.back = list[4]
    self.bottom = list[5]

    self.dic = {}
    for i in xrange(6):
      if list[i] in self.dic:
        self.dic[list[i]] += 1
      else:
        self.dic[list[i]] = 1

  def print_all(self):
    print "top = " + self.top + ", front = " + self.front + ", right = " + self.right + ", left = " + self.left + ", back = " + self.back + ", bottom = " + self.bottom

  def roll_N(self):
    temp = self.top
    self.top = self.front
    self.front = self.bottom
    self.bottom = self.back
    self.back = temp

  def roll_S(self):
    temp = self.top
    self.top = self.back
    self.back = self.bottom
    self.bottom = self.front
    self.front = temp

  def roll_W(self):
    temp = self.top
    self.top = self.right
    self.right = self.bottom
    self.bottom = self.left
    self.left = temp

  def roll_E(self):
    temp = self.top
    self.top = self.left
    self.left = self.bottom
    self.bottom = self.right
    self.right = temp

  def roll_C(self):
    temp = self.front
    self.front = self.right
    self.right = self.back
    self.back = self.left
    self.left = temp

  def random_roll(self):
    ram = random.randint(1, 4)
    if ram == 1:
      self.roll_E()
    elif ram == 2:
      self.roll_N()
    elif ram == 3:
      self.roll_S()
    else:
      self.roll_W()

  def getDifference(self, dice):
    same_list = []
    different_list = []
    if self.top == dice.top:
      same_list.append("top")
    else:
      different_list.append("top")
    if self.front == dice.front:
      same_list.append("front")
    else:
      different_list.append("front")
    if self.left == dice.left:
      same_list.append("left")
    else:
      different_list.append("left")
    if self.back == dice.back:
      same_list.append("back")
    else:
      different_list.append("back")
    if self.right == dice.right:
      same_list.append("right")
    else:
      different_list.append("right")
    if self.bottom == dice.bottom:
      same_list.append("bottom")
    else:
      different_list.append("bottom")

    result_list = [same_list, different_list]
    return result_list

  def getList(self):
    list = [self.top, self.front, self.right, self.left, self.back, self.bottom]
    return list

def print_dices():
  print "first dice: "
  first_dice.print_all()
  print "second dice: "
  second_dice.print_all()
  

line = "


def judgeDices(first_dice, second_dice):
  flag = False
  num = 0
  debug = 0
  while num < 4:
    
    
    
    

    dic1_keys = first_dice.dic.keys()
    
    for i in xrange(len(dic1_keys)):
      
      
      if not (dic1_keys[i] in second_dice.dic):
        
        num = 4
        break
      elif first_dice.dic[dic1_keys[i]] != second_dice.dic[dic1_keys[i]]:
        num = 4
        break

    if num >=4:
      
      break

    for i in xrange(len(dic1_keys)):
      if first_dice.dic[dic1_keys[i]] == 6 or first_dice.dic[dic1_keys[i]] == 5:
        num = 4
        flag = True
        break

    
    if num >=4:
      break

    diferece = first_dice.getDifference(second_dice)
    

    same_num = len(diferece[0])
    if same_num == 6:
      break
    elif same_num == 5:
      num = 4
      break

    if "front" in diferece[0] or "back" in diferece[0]:
      first_dice.roll_W()
      num += 1
    elif "left" in diferece[0] or "right" in diferece[0]:
      first_dice.roll_S()
      num += 1
    elif "top" in diferece[0] or "bottom" in diferece[0]:
      first_dice.roll_C()
      num += 1
    else:
      first_dice.random_roll()

  if flag == True:
    return True
  elif num == 4:
    return False
  else:
    return True

n = int(raw_input())
dices = []
for i in xrange(n):
  dices.append(Dice(raw_input().split(" ")))
result = False
for i in xrange(n - 1):
  for j in xrange(i + 1, n):
    first_dice = dices[i]
    second_dice = dices[j]
    
    
    
    
    
    result = judgeDices(first_dice, second_dice)
    
    if result:
      break
  if result:
    break

if result:
  print "No"
else:
  print "Yes"

Solution (Python):



POSITIONS = ["top", "south", "east", "west", "north", "bottom"]


class Dice(object):

    def __init__(self, initial_faces):
        self.faces = {p: initial_faces[i] for i, p in enumerate(POSITIONS)}
        self.store_previous_faces()

    def store_previous_faces(self):
        self.previous_faces = self.faces.copy()

    def change_face(self, after, before):
        self.faces[after] = self.previous_faces[before]

    def change_faces(self, rotation):
        self.change_face(rotation[0], rotation[1])
        self.change_face(rotation[1], rotation[2])
        self.change_face(rotation[2], rotation[3])
        self.change_face(rotation[3], rotation[0])

    def roll(self, direction):
        self.store_previous_faces()
        if direction == "E":
            self.change_faces(["top", "west", "bottom", "east"])
        elif direction == "N":
            self.change_faces(["top", "south", "bottom", "north"])
        elif direction == "S":
            self.change_faces(["top", "north", "bottom", "south"])
        elif direction == "W":
            self.change_faces(["top", "east", "bottom", "west"])

    def issame(self, dice1):
        if set(self.faces.values()) != set(dice1.faces.values()):
            return False
        if (dice1.faces["south"] == self.faces["west"] or dice1.faces["south"] == self.faces["east"]):
            self.roll("E")
        while dice1.faces["south"] != self.faces["south"]:
            self.roll("N")
        for n in range(4):
            if (dice1.faces == self.faces):
                
                
                return True
                break
            else:
                self.roll("W")
        else:
            return False


def isalldifferent(dices):
    n = len(dices)
    for i in range(n):
        for j in range(i + 1, n):
            
            if dices[i].issame(dices[j]):
                return False
                break
    else:
        return True


def main():
    n = int(input())
    dices = [Dice(input().split()) for i in range(n)]
    print("Yes" if isalldifferent(dices) else "No")


if __name__ == "__main__":
    main()