Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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 in JAVA (judge verdict: Accepted)

import java.util.*;
import java.io.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = Integer.parseInt(sc.next());
        Dice[] dices = new Dice[n];
        for (int i = 0; i < n; i++) {
            // [1-1]:top
            // [2-1]:south
            // [3-1]:east
            // [4-1]:west
            // [5-1]:north
            // [6-1]:down
            int[] a = new int[6];
            for (int j = 0; j < 6; j++) {
                a[j] = Integer.parseInt(sc.next());
            }
            dices[i] = new Dice(a[0], a[1], a[2], a[3], a[4], a[5]);
        }

        boolean equalExist = false;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (dices[i].equals(dices[j])) {
                    equalExist = true;
                    break;
                }
            }
        }
        if (equalExist) {
            out.printf("No\n");
        } else {
            out.printf("Yes\n");
        }
        out.flush();
    }
}

class Dice {
    private int top;
    private int south;
    private int east;
    private int west;
    private int north;
    private int down;
    Dice(int in_top, int in_south, int in_east, int in_west, int in_north, int in_down) {
        top = in_top;
        south = in_south;
        east = in_east;
        west = in_west;
        north = in_north;
        down = in_down;
    }
    public boolean rotation(int top_val, int south_val) {
        if (top == top_val && south == south_val) {
            return true;
        }
        if (south != south_val) {
            if (top == south_val) {
                this.toSouth();
            } else if (east == south_val) {
                this.toWest();
                this.toSouth();
            } else if (west == south_val) {
                this.toEast();
                this.toSouth();
            } else if (north == south_val) {
                this.toSouth();
                this.toSouth();
            } else if (down == south_val) {
                this.toNorth();
            } else {
                return false;
            }
        } else {
            if (east == top_val) {
                this.toWest();
            } else if (west == top_val) {
                this.toEast();
            } else if (down == top_val) {
                this.toWest();
                this.toWest();
            } else {
                return false;
            }
        }
        return rotation(top_val, south_val);
    }
    public boolean equals(Dice dice) {
        if (this.rotation(dice.getTop(), dice.getSouth())) {
            if (this.north == dice.north &&
                this.east == dice.east &&
                this.west == dice.west &&
                this.down == dice.down) {
                return true;
            }
        }
        return false;
    }
    public void toNorth() {
        int temp = top;
        top = south;
        south = down;
        down = north;
        north = temp;
    }
    public void toSouth() {
        int temp = top;
        top = north;
        north = down;
        down = south;
        south = temp;
    }
    public void toEast() {
        int temp = top;
        top = west;
        west = down;
        down = east;
        east = temp;
    }
    public void toWest() {
        int temp = top;
        top = east;
        east = down;
        down = west;
        west = temp;
    }
    public int getTop() {
        return top;
    }
    public int getSouth() {
        return south;
    }
    public int getEast() {
        return east;
    }
}