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,pips):
        self.top = pips[0]
        self.south = pips[1]
        self.east = pips[2]
        self.west = pips[3]
        self.north =pips[4]
        self.bottom=pips[5]
    def n(self):
        self.top,self.south,self.bottom,self.north = \
        self.south,self.bottom,self.north,self.top
    def s(self):
        self.top,self.north,self.bottom,self.south = \
        self.north,self.bottom,self.south,self.top
    def e(self):
        self.top,self.west,self.bottom,self.east = \
        self.west,self.bottom,self.east,self.top
    def w(self):
        self.top,self.east,self.bottom,self.west = \
        self.east,self.bottom,self.west,self.top
    def r(self):
        self.north,self.east,self.south,self.west = \
        self.east,self.south,self.west,self.north
    def set_ts(self,t,s):
        for i in range(4):
            if self.top != t:
                if self.east == t or self.west == t:
                    self.e()
                else:
                    self.n()
            else:
                break
        for i in range(4):
            if self.south != s:
                self.r()
            else:
                break
        if(self.top == t and self.south == s):
            return True
        else:
            return False
    def gen_pattern(self):
        pat = []
        for i in range(6):
            if i < 4:
                self.n()
            elif i == 4:
                self.e()
            elif i == 5:
                self.e()
                self.e()
            for j in range(4):
                self.r()
                pat.append([self.top,self.north,self.south,self.east,self.west,self.bottom])

        return pat


n = int(input())
dices = []
pat = []
for i in range(n):
    pips = [int(x) for x in input().split()]
    dices.append(Dice(pips))
    pat.append(sorted(dices[i].gen_pattern()))
pat_uniq = []
for x in pat:
    if x not in pat_uniq:
        pat_uniq.append(x)

if len(pat) == len(pat_uniq):
    print("Yes")
else:
    print("No")

Solution (Python):
import itertools
class Dice:
    def __init__(self, labels):
        self.labels = labels
        self.up = labels[0]
        self.front = labels[1]
        self.right = labels[2]
        self.left = labels[3]
        self.back = labels[4]
        self.down = labels[5]
    def east(self):
        self.up, self.right, self.down, self.left = self.left, self.up, self.right, self.down
        self.update_labels()
    def north(self):
        self.up, self.back, self.down, self.front = self.front, self.up, self.back, self.down
        self.update_labels()
    def south(self):
        self.up, self.front, self.down, self.back = self.back, self.up, self.front, self.down
        self.update_labels()
    def west(self):
        self.up, self.left, self.down, self.right = self.right, self.up, self.left, self.down
        self.update_labels()
    def clockwise(self):
        self.front, self.left, self.back, self.right = self.right, self.front, self.left, self.back
        self.update_labels()
    def counterclockwise(self):
        self.front, self.right, self.back, self.left = self.left, self.front, self.right, self.back
        self.update_labels()
    def set_up(self, up):
        for i in range(6):
            if up == self.up:
                break
            if i % 2:
                self.east()
            else:
                self.north()
    def set_front(self, front):
        while True:
            if front == self.front:
                break
            self.clockwise()
    def update_labels(self):
        self.labels = [self.up, self.front, self.right, self.left, self.back, self.down]

def is_the_same(d1, d2):
    for i in range(30):
        if d1.labels == d2.labels:
            return True
        if i % 5 == 0:
            if i % 10 == 0:
                d2.east()
            else:
                d2.north()
        else:
            d2.clockwise()
    else:
        return False

n = int(input())
dices = []
for _ in range(n):
    dices.append(Dice(list(map(int, input().split()))))
for d in itertools.combinations(dices, 2):
    if is_the_same(d[0], d[1]):
        print('No')
        break
else:
    print('Yes')

Solution (Python):
class Dice():

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

    def north(self):
        self.change([2, 6, 3, 4, 1, 5])

    def west(self):
        self.change([3, 2, 6, 1, 5, 4])

    def east(self):
        self.change([4, 2, 1, 6, 5, 3])

    def south(self):
        self.change([5, 1, 3, 4, 6, 2])

    def rotate(self):
        self.change([1, 4, 2, 5, 3, 6])

    def change(self, convert):
        num = []
        for i in range(6):
            num.append(self.label[convert[i] - 1])
        self.label = num

    def is_same_dice(self, another_dice):
        for i in range(6):
            if i % 2 == 0:
                self.north()
            else:
                self.west()
            for j in range(4):
                self.rotate()
                if self.label == another_dice:
                    return True
        return False


def main():
    dices = []
    n = int(input())
    for i in range(n):
        dices.append(Dice([int(x) for x in input().split()]))

    flag = False
    for i in range(n):
        j = i + 1
        while j < n:
            if dices[i].is_same_dice(dices[j].label):
                flag = True
                break
            j += 1

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


if __name__ == '__main__':
    main()

Solution (Python):
def N(Dice):
    num=Dice[0]
    Dice[0]=Dice[1]
    Dice[1]=Dice[5]
    Dice[5]=Dice[4]
    Dice[4]=num
def E(Dice):
    num=Dice[0]
    Dice[0]=Dice[3]
    Dice[3]=Dice[5]
    Dice[5]=Dice[2]
    Dice[2]=num
def W(Dice):
    num=Dice[0]
    Dice[0]=Dice[2]
    Dice[2]=Dice[5]
    Dice[5]=Dice[3]
    Dice[3]=num
def S(Dice):
    num=Dice[0]
    Dice[0]=Dice[4]
    Dice[4]=Dice[5]
    Dice[5]=Dice[1]
    Dice[1]=num
def DICE(Dice,Dice2):
    if (Dice[0] in Dice2)==False:
        """print('No')"""
        return 0
    else:
        for j in range(6):
            if j%2==0:
                N(Dice2)
            else:
                W(Dice2)
            for i in range(4):
                N(Dice2)
                W(Dice2)
                S(Dice2)
                if Dice==Dice2:
                    break
            if Dice==Dice2:
                break
    if Dice==Dice2:
        """print('Yes')"""
        return 1
    else:
        """print('No')"""
        return 0
cnt=0             
a=int(input())
Dice=[i for i in range(a)]
for i in range(a):
     Dice[i]=[int(i) for i in input().split()]
for i in range(a-1):
    for j in range(i+1,a):
        cnt+=DICE(Dice[i],Dice[j])
if cnt==0:
    print("Yes")
else:
    print("No")

Solution (Python):
def N(Dice):
    num=Dice[0]
    Dice[0]=Dice[1]
    Dice[1]=Dice[5]
    Dice[5]=Dice[4]
    Dice[4]=num
def E(Dice):
    num=Dice[0]
    Dice[0]=Dice[3]
    Dice[3]=Dice[5]
    Dice[5]=Dice[2]
    Dice[2]=num
def W(Dice):
    num=Dice[0]
    Dice[0]=Dice[2]
    Dice[2]=Dice[5]
    Dice[5]=Dice[3]
    Dice[3]=num
def S(Dice):
    num=Dice[0]
    Dice[0]=Dice[4]
    Dice[4]=Dice[5]
    Dice[5]=Dice[1]
    Dice[1]=num
def DICE(Dice,Dice2):
    if (Dice[0] in Dice2)==False:
        """print('No')"""
        return 0
    else:
        for j in range(6):
            if j%2==0:
                N(Dice2)
            else:
                W(Dice2)
            for i in range(4):
                N(Dice2)
                W(Dice2)
                S(Dice2)
                if Dice==Dice2:
                    break
            if Dice==Dice2:
                break
    if Dice==Dice2:
        """print('Yes')"""
        return 1
    else:
        """print('No')"""
        return 0
cnt=0             
a=int(input())
Dice=[i for i in range(a)]
for i in range(a):
     Dice[i]=[int(i) for i in input().split()]
for i in range(a-1):
    for j in range(i+1,a):
        cnt+=DICE(Dice[i],Dice[j])
if cnt==0:
    print("Yes")
else:
    print("No")

Solution (Python):
def N(Dice):
    num=Dice[0]
    Dice[0]=Dice[1]
    Dice[1]=Dice[5]
    Dice[5]=Dice[4]
    Dice[4]=num
def E(Dice):
    num=Dice[0]
    Dice[0]=Dice[3]
    Dice[3]=Dice[5]
    Dice[5]=Dice[2]
    Dice[2]=num
def W(Dice):
    num=Dice[0]
    Dice[0]=Dice[2]
    Dice[2]=Dice[5]
    Dice[5]=Dice[3]
    Dice[3]=num
def S(Dice):
    num=Dice[0]
    Dice[0]=Dice[4]
    Dice[4]=Dice[5]
    Dice[5]=Dice[1]
    Dice[1]=num
def DICE(Dice,Dice2):
    if (Dice[0] in Dice2)==False:
        """print('No')"""
        return 0
    else:
        for j in range(6):
            if j%2==0:
                N(Dice2)
            else:
                W(Dice2)
            for i in range(4):
                N(Dice2)
                W(Dice2)
                S(Dice2)
                if Dice==Dice2:
                    break
            if Dice==Dice2:
                break
    if Dice==Dice2:
        """print('Yes')"""
        return 1
    else:
        """print('No')"""
        return 0
cnt=0             
a=int(input())
Dice=[i for i in range(a)]
for i in range(a):
     Dice[i]=[int(i) for i in input().split()]
for i in range(a-1):
    for j in range(i+1,a):
        cnt+=DICE(Dice[i],Dice[j])
if cnt==0:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys
class Dice:
    def __init__(self, f):
        self.f = f[:]

    def roll(self, s):
        for f in s:
            n = self.f[:]
            if f == 'E':
                t = n[0]
                n[0] = n[3]
                n[3] = n[5]
                n[5] = n[2]
                n[2] = t
            elif f == 'W':
                t = n[0]
                n[0] = n[2]
                n[2] = n[5]
                n[5] = n[3]
                n[3] = t
            elif f == 'N':
                t = n[0]
                n[0] = n[1]
                n[1] = n[5]
                n[5] = n[4]
                n[4] = t
            else:
                t = n[0]
                n[0] = n[4]
                n[4] = n[5]
                n[5] = n[1]
                n[1] = t
            self.f = n

    def to_top(self, index):
        if index == 1:
            self.roll('N')
        elif index == 2:
            self.roll('W')
        elif index == 3:
            self.roll('E')
        elif index == 4:
            self.roll('S')
        elif index == 5:
            self.roll('NN')
        return self

    def side_roll(self):
        t  = self.f[1]
        self.f[1] = self.f[2]
        self.f[2] = self.f[4]
        self.f[4] = self.f[3]
        self.f[3] = t
        return self

    def all_face(self):
        faces = []
        for i in range(0,6):
            t = Dice(self.f[:])
            faces.append(t.to_top(i).f[:])
            for j in range(3):
                faces.append(t.side_roll().f[:])
        return faces

n = int(input())
ds = list(Dice(list(input().split())) for _ in range(n))
e = False
for i, d in enumerate(ds):
    for d2 in ds[i+1:]:
        if d.f in d2.all_face() : e = True 
print('No' if e else 'Yes')

Solution (Python):
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def roll_dice(self, str):
        """

        Args:
            str: move direction

        Returns:

        """
        for s in str:
            if s == 'N':
                self.move_north()
            elif s == 'S':
                self.move_south()
            elif s == 'W':
                self.move_west()
            elif s == 'E':
                self.move_east()

    def set_top(self, value):
        """
        Args:
            value: target_value
        """
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("S")
            counter += 1
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("W")
            counter += 1

    def set_front(self, value):
        """
        Args:
            value: target value
        """
        counter = 0
        while counter < 4:
            if self.vertical[1] == value:
                self.map_values()
                return
            else:
                self.roll_dice("SWN")
            counter += 1

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def map_values(self):
        self.numbers[1] = self.vertical[0]
        self.numbers[2] = self.vertical[1]
        self.numbers[3] = self.horizontal[2]
        self.numbers[4] = self.horizontal[0]
        self.numbers[5] = self.vertical[3]
        self.numbers[6] = self.vertical[2]

    def get_top(self):
        return self.vertical[0]


def is_same(dice1, dice2):
    """compare two dices

    Args:
        dice1: dice1
        dice2: dice2

    Returns:
        Bool (whether dice1 is equals to be dice2 or not)
    """
    flag = False
    if dice1.numbers == dice2.numbers:
        flag = True
    else:
        for i in range(6):
            dice1.set_top(i + 1)
            for j in xrange(4):
                dice1.roll_dice("SWN")
                if dice1.numbers == dice2.numbers:
                    flag = True
            dice1.roll_dice("S")

    return flag


dice_number = int(raw_input())
dices = []
counter = 0
while counter < dice_number:
    dices.append(Dice([int(x) for x in raw_input().split()]))
    counter += 1

ans = 0
for i in range(dice_number - 1):
    for j in range(i + 1, dice_number):
        ans += int(is_same(dices[i], dices[j]))

if ans > 0:
    print("No")
else:
    print("Yes")

Solution (Python):
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def roll_dice(self, str):
        """

        Args:
            str: move direction

        Returns:

        """
        for s in str:
            if s == 'N':
                self.move_north()
            elif s == 'S':
                self.move_south()
            elif s == 'W':
                self.move_west()
            elif s == 'E':
                self.move_east()

    def set_top(self, value):
        """
        Args:
            value: target_value
        """
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("S")
            counter += 1
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("W")
            counter += 1

    def set_front(self, value):
        """
        Args:
            value: target value
        """
        counter = 0
        while counter < 4:
            if self.vertical[1] == value:
                self.map_values()
                return
            else:
                self.roll_dice("SWN")
            counter += 1

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def map_values(self):
        self.numbers[1] = self.vertical[0]
        self.numbers[2] = self.vertical[1]
        self.numbers[3] = self.horizontal[2]
        self.numbers[4] = self.horizontal[0]
        self.numbers[5] = self.vertical[3]
        self.numbers[6] = self.vertical[2]

    def get_top(self):
        return self.vertical[0]


def is_same(dice1, dice2):
    """compare two dices

    Args:
        dice1: dice1
        dice2: dice2

    Returns:
        Bool (whether dice1 is equals to be dice2 or not)
    """
    flag = False
    if dice1.numbers == dice2.numbers:
        flag = True
    else:
        for i in range(6):
            dice1.set_top(i + 1)
            for j in xrange(4):
                dice1.roll_dice("SWN")
                if dice1.numbers == dice2.numbers:
                    flag = True
            dice1.roll_dice("S")

    return flag


dice_number = int(raw_input())
dices = []
counter = 0
while counter < dice_number:
    dices.append(Dice([int(x) for x in raw_input().split()]))
    counter += 1

ans = 0
for i in range(dice_number - 1):
    if ans > 0:
        break
    for j in range(i + 1, dice_number):
        ans += int(is_same(dices[i], dices[j]))
        if ans > 0:
            print("No")
            break

if ans == 0:
    print("Yes")

Solution (Python):
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def roll_dice(self, str):
        """

        Args:
            str: move direction

        Returns:

        """
        for s in str:
            if s == 'N':
                self.move_north()
            elif s == 'S':
                self.move_south()
            elif s == 'W':
                self.move_west()
            elif s == 'E':
                self.move_east()

    def set_top(self, value):
        """
        Args:
            value: target_value
        """
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("S")
            counter += 1
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("W")
            counter += 1

    def set_front(self, value):
        """
        Args:
            value: target value
        """
        counter = 0
        while counter < 4:
            if self.vertical[1] == value:
                self.map_values()
                return
            else:
                self.roll_dice("SWN")
            counter += 1

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def map_values(self):
        self.numbers[1] = self.vertical[0]
        self.numbers[2] = self.vertical[1]
        self.numbers[3] = self.horizontal[2]
        self.numbers[4] = self.horizontal[0]
        self.numbers[5] = self.vertical[3]
        self.numbers[6] = self.vertical[2]

    def get_top(self):
        return self.vertical[0]


def is_same(dice1, dice2):
    """compare two dices

    Args:
        dice1: dice1
        dice2: dice2

    Returns:
        Bool (whether dice1 is equals to be dice2 or not)
    """
    flag = False
    if dice1.numbers == dice2.numbers:
        flag = True
    else:
        for i in range(6):
            dice1.set_top(i + 1)
            for j in xrange(4):
                dice1.roll_dice("SWN")
                if dice1.numbers == dice2.numbers:
                    flag = True
            dice1.roll_dice("S")

    return flag


dice_number = int(raw_input())
dices = []
counter = 0
while counter < dice_number:
    dices.append(Dice([int(x) for x in raw_input().split()]))
    counter += 1

ans = 0
for i in range(dice_number - 1):
    if ans > 0:
        break
    for j in range(i + 1, dice_number):
        ans += int(is_same(dices[i], dices[j]))
        if ans > 0:
            print("No")
            break

if ans == 0:
    print("Yes")

Solution (Python):
class Dice(object):
    """Dice Class

    """

    def __init__(self, numbers):
        """

        Args:
            numbers:
        """
        self.numbers_inverse = {numbers[0]: 1, numbers[1]: 2, numbers[2]: 3, numbers[3]: 4, numbers[4]: 5,
                                numbers[5]: 6}
        self.numbers = {1: numbers[0], 2: numbers[1], 3: numbers[2], 4: numbers[3], 5: numbers[4], 6: numbers[5]}

        self.vertical = [self.numbers[1], self.numbers[2], self.numbers[6], self.numbers[5]]
        self.horizontal = [self.numbers[4], self.numbers[1], self.numbers[3], self.numbers[6]]

    def roll_dice(self, str):
        """

        Args:
            str: move direction

        Returns:

        """
        for s in str:
            if s == 'N':
                self.move_north()
            elif s == 'S':
                self.move_south()
            elif s == 'W':
                self.move_west()
            elif s == 'E':
                self.move_east()

    def set_top(self, value):
        """
        Args:
            value: target_value
        """
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("S")
            counter += 1
        counter = 0
        while counter < 4:
            if self.vertical[0] == value:
                self.map_values()
                return
            else:
                self.roll_dice("W")
            counter += 1

    def set_front(self, value):
        """
        Args:
            value: target value
        """
        counter = 0
        while counter < 4:
            if self.vertical[1] == value:
                self.map_values()
                return
            else:
                self.roll_dice("SWN")
            counter += 1

    def move_south(self):
        """move this dice towered north
        """
        self.vertical = (self.vertical * 2)[3:7]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_north(self):
        """move this dice towered south
        """
        self.vertical = (self.vertical * 2)[1:5]
        self.horizontal[1] = self.vertical[0]
        self.horizontal[3] = self.vertical[2]
        self.map_values()

    def move_east(self):
        """move this dice towered east
        """
        self.horizontal = (self.horizontal * 2)[3:7]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def move_west(self):
        """move this dice towered west
        """
        self.horizontal = (self.horizontal * 2)[1:5]
        self.vertical[0] = self.horizontal[1]
        self.vertical[2] = self.horizontal[3]
        self.map_values()

    def map_values(self):
        self.numbers[1] = self.vertical[0]
        self.numbers[2] = self.vertical[1]
        self.numbers[3] = self.horizontal[2]
        self.numbers[4] = self.horizontal[0]
        self.numbers[5] = self.vertical[3]
        self.numbers[6] = self.vertical[2]

    def get_top(self):
        return self.vertical[0]


def is_same(dice1, dice2):
    """compare two dices

    Args:
        dice1: dice1
        dice2: dice2

    Returns:
        Bool (whether dice1 is equals to be dice2 or not)
    """
    flag = False
    if dice1.numbers == dice2.numbers:
        flag = True
    elif set(dice1.numbers_inverse.keys()) != set(dice2.numbers_inverse.keys()):
        flag = False
    else:
        for i in range(6):
            dice1.set_top(i + 1)
            for j in xrange(4):
                dice1.roll_dice("SWN")
                if dice1.numbers == dice2.numbers:
                    flag = True
            dice1.roll_dice("S")

    return flag


dice_number = int(raw_input())
dices = []
counter = 0
while counter < dice_number:
    dices.append(Dice([int(x) for x in raw_input().split()]))
    counter += 1

ans = 0
for i in range(dice_number - 1):
    if ans > 0:
        break
    for j in range(i + 1, dice_number):
        ans += int(is_same(dices[i], dices[j]))
        if ans > 0:
            print("No")
            break

if ans == 0:
    print("Yes")

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

dice = [[0 for i in range(6)] for j in range(n)]
for i in range(n):
    dice[i] = map(int, raw_input().split())
 
def rolling(inst, dice):
    if inst == 'E':
        dice[5], dice[2], dice[0], dice[3] = dice[2], dice[0], dice[3], dice[5]
    elif inst == 'W':
        dice[5], dice[3], dice[0], dice[2] = dice[3], dice[0], dice[2], dice[5]
    elif inst == 'N':
        dice[5], dice[4], dice[0], dice[1] = dice[4], dice[0], dice[1], dice[5]
    elif inst == 'S':
        dice[5], dice[1], dice[0], dice[4] = dice[1], dice[0], dice[4], dice[5]
 
def dice_matching(dice1, dice2):
    if dice1[1] == dice2[1]:
        for i in range(4):
            if dice1 == dice2:
                return 1
                break
            rolling('E', dice2)
        return 0
    else:
        return 0
        
flag = 0
for i in range(n):
    for j in range(i+1, n):
        count = 0
        while(1):
            if dice_matching(dice[i], dice[j]) == 1:
                flag = 1
                print 'No'
                break
            else:
                if count < 3:
                    rolling('N', dice[j])
                    count += 1
                elif 7 > count and count >= 3:
                    if count == 3:
                        rolling('E', dice[j])
                    rolling('N', dice[j])
                    count += 1
                else:
                    break
        if flag ==1:
            break
    if flag == 1:
        break
if flag == 0:
    print 'Yes'

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

dice = [[0 for i in range(6)] for j in range(n)]
for i in range(n):
    dice[i] = map(int, raw_input().split())
 
def rolling(inst, dice):
    if inst == 'E':
        dice[5], dice[2], dice[0], dice[3] = dice[2], dice[0], dice[3], dice[5]
    elif inst == 'W':
        dice[5], dice[3], dice[0], dice[2] = dice[3], dice[0], dice[2], dice[5]
    elif inst == 'N':
        dice[5], dice[4], dice[0], dice[1] = dice[4], dice[0], dice[1], dice[5]
    elif inst == 'S':
        dice[5], dice[1], dice[0], dice[4] = dice[1], dice[0], dice[4], dice[5]
 
def dice_matching(dice1, dice2):
    if dice1[1] == dice2[1]:
        for i in range(4):
            if dice1 == dice2:
                return 1
                break
            rolling('E', dice2)
        return 0
    else:
        return 0

def alldice_matching(dice1, dice2):
    count = 0
    while(1):
        if dice_matching(dice1, dice2) == 1:
            return 1
        else:
            if count < 3:
                rolling('N', dice2)
                count += 1
            elif 7 > count and count >= 3:
                if count == 3:
                    rolling('E', dice2)
                rolling('N', dice2)
                count += 1
            else:
                return 0

flag = 0
for i in range(n):
    for j in range(i+1, n):
        if alldice_matching(dice[i], dice[j]) == 1:
            flag = 1
            print "No"
            break
    if flag == 1:
        break
if flag == 0:
    print 'Yes'