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

<?php

// ITP1_11 Dice IV

// read data
$n = trim(fgets(STDIN));
$data = [];
for ($i = 0; $i < $n; $i++) {
  $input = trim(fgets(STDIN));
  $labels = explode(" ", $input);
  $data[] = $labels;
}

// usort($data, 'compare');

// check
$cmp = false;
for ($i = 0; $i < $n-1 and !$cmp; $i++) {
  $dice1 = new Dice($data[$i]);
  // $a = $data[$i];
  // sort($a);
  for ($j = $i + 1; $j < $n and !$cmp; $j++) {
    // $b = $data[$j];
    // sort($b);
    //if (!array_diff_assoc($a, $b)) {
      $dice2 = new Dice($data[$j]);
      $cmp = compare_dice($dice1, $dice2);
      // var_dump($cmp);
    //}
  }
}

// answer
if ($cmp) {
  echo "No" . PHP_EOL;
} else {
  echo "Yes" . PHP_EOL;
}

//========================================

function print_labels(array $data) {
  foreach ($data as $labels) {
    echo implode(",", $labels) . PHP_EOL;
  }
}

function compare($arr_a, $arr_b)
{
  $sum_a = array_sum($arr_a);
  $sum_b = array_sum($arr_b);

  if ($sum_a == $sum_b) {
    return 0;
  } else {
    return ($sum_a < $sum_b) ? -1 : 1;
  }
}

function compare_dice($dice1, $dice2)
{
  $dice1_top = $dice1->getLabel("T");

  $check_dirs = "NWSSSW";
  foreach (str_split($check_dirs) as $dir)
  {
    $dice2->roll($dir);
    $dice2_top = $dice2->getLabel("T");
    if ($dice1_top == $dice2_top) {
      for ($i = 0; $i < 4; $i++) {
        if ($dice1->isEqual($dice2)) {
          return true;
        }
        $dice2->roll("R");
      }
    }
  }
  return false;
}


//========================================

class Dice
{
  private $labels;
  private $TBL_ROLL = array(
    array(4, 0, 2, 3, 5, 1),  // -> S
    array(3, 1, 0, 5, 4, 2),  // -> E
    array(2, 1, 5, 0, 4, 3),  // -> W
    array(1, 5, 2, 3, 0, 4),  // -> N
    array(0, 3, 1, 4, 2, 5),  // -> sidespin : right
    array(0, 2, 4, 1, 3, 5)   // -> sidespin : left
  );

  public function __construct($labels)
  {
    $this->labels = $labels;
  }

  public function roll($dirs)
  {
    foreach (str_split($dirs) as $dir) {
      $idx = strpos("SEWNRL", $dir);
      if ($idx === false) {
        continue;
      }

      $old = $this->labels;
      for ($i = 0; $i < 6; $i++) {
        $this->labels[$i] = $old[$this->TBL_ROLL[$idx][$i]];
      }
    }
  }

  public function isEqual($dice) {
    return ($this->labels == $dice->getLabels());
  }

  public function getLabel($dir) {
    $idx = strpos("TSEWNB", $dir);
    if ($idx !== false) {
      return $this->labels[$idx];
    }
    return false;
  }

  public function getLabels() {
    return $this->labels;
  }

  public function getDirection($number) {
    $idx = array_search($number, $this->labels);
    if ($idx !== false) {
      return "TSEWNB"[$idx];
    }
    return false;
  }
}