Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
		"reflect"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	// 上面の色を固定してから、左右方向に回転して一致するものがあるかを調べる
	round := [][]int{
		{1,2,3,4,5,6}, {1,3,5,2,4,6}, {1,4,2,5,3,6}, {1,5,4,3,2,6}, // c1を上面に固定して回転
		{2,6,3,4,1,5}, {2,3,1,6,4,5}, {2,1,4,3,6,5}, {2,4,6,1,3,5}, // c2を上面に固定して回転
		{3,1,2,5,6,4}, {3,2,6,1,5,4}, {3,5,1,6,2,4}, {3,6,5,2,1,4}, // c3を上面に固定して回転
		{4,1,5,2,6,3}, {4,2,1,6,5,3}, {4,5,6,1,2,3}, {4,6,2,5,1,3}, // c4を上面に固定して回転
		{5,1,3,4,6,2}, {5,3,6,1,4,2}, {5,4,1,6,3,2}, {5,6,4,3,1,2}, // c5を上面に固定して回転
		{6,2,4,3,5,1}, {6,3,2,5,4,1}, {6,5,3,4,2,1}, {6,4,5,2,3,1}, // c6を上面に固定して回転

	}
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		cubes := make([][]string, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			cubes[i] = strings.Split(scanner.Text(), " ")
		}
		check := func(a, b int) bool {
			tmp := make([]string, 6)
			for i := 0; i < 24; i++ {
				for j := 0; j < 6; j++ {
					tmp[j] = cubes[b][round[i][j]-1]
				}
				if reflect.DeepEqual(tmp, cubes[a]) {
					return true
				}
			}
			return false
		}
		tbl := make([]int, n)
		// 全ての組み合わせを調べる
		for i := 0; i < n; i++ {
			if tbl[i] == 1 {
				continue
			}
			for j := i+1; j < n; j++ {
				if check(i, j) {
					tbl[j] = 1
				}
			}
		}
		fmt.Println(sum(tbl))
	}
}

func sum(a []int) int {
	ret := 0
	for _, v := range a {
		ret += v
	}
	return ret
}

Python: D = [
    (1, 5, 2, 3, 0, 4), # 'U'
    (3, 1, 0, 5, 4, 2), # 'R'
    (4, 0, 2, 3, 5, 1), # 'D'
    (2, 1, 5, 0, 4, 3), # 'L'
]
p_dice = (0, 0, 0, 1, 1, 2, 2, 3)*3

def rotate_dice(L0):
    L = L0[:]
    for k in p_dice:
        yield L
        L[:] = (L[e] for e in D[k])

while 1:
    N = int(input())
    if N == 0:
        break
    res = []
    for i in range(N):
        cube = input().split()
        for dice in rotate_dice(cube):
            if dice in res:
                break
        else:
            res.append(cube)
    print(N - len(res))

Python: import copy


class Dice:
    def __init__(self, a, b, c, d, e, f):
        self.top = a
        self.front = b
        self.right = c
        self.left = d
        self.behind = e
        self.bottom = f

    def S(self):
        self.top, self.front, self.behind, self.bottom = self.behind, self.top, self.bottom, self.front

    def N(self):
        self.behind, self.top, self.bottom, self.front = self.top, self.front, self.behind, self.bottom

    def E(self):
        self.top, self.bottom, self.left, self.right = self.left, self.right, self.bottom, self.top

    def W(self):
        self.left, self.right, self.bottom, self.top = self.top, self.bottom, self.left, self.right

    def spin(self):
        self.left, self.behind, self.right, self.front = self.front, self.left, self.behind, self.right

    def __eq__(self, _dice2):
        dice2 = copy.deepcopy(_dice2)
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.N()
        dice2.N()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.N()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.E()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.W()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        dice2 = copy.deepcopy(_dice2)
        dice2.S()
        for i in range(4):
            if self._eqaul(dice2):
                return True
            dice2.spin()
        return False

    def _eqaul(self, dice2):
        return self.front == dice2.front and self.behind == dice2.behind and self.left == dice2.left and self.right == dice2.right and self.top == dice2.top and self.bottom == dice2.bottom

    def __repr__(self):
        return ",".join([str(object=i) for i in (self.top, self.front, self.right, self.left, self.behind, self.bottom)])


while True:
    n=int(input())
    if n==0:
        break
    dices=[]
    for i in range(n):
        c=input().split(" ")
        dice=Dice(*c)
        if (dice in dices)==False:
            dices.append(dice)
    print(n-len(dices))

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

def solve():
    from sys import stdin
    f_i = stdin
    
    ans_list = []
    
    while True:
        n = int(f_i.readline())
        
        if n == 0:
            break
        
        dices = [Dice(f_i.readline().split()) for i in range(n)]
        
        ans = 0
        while dices:
            tmp_dices = []
            d1 = dices.pop()
            for d2 in dices:
                if d1.is_same(d2):
                    ans += 1
                else:
                    tmp_dices.append(d2)
            dices = tmp_dices
        
        ans_list.append(ans)
    
    print('\n'.join(map(str, ans_list)))

solve()

Python: roll = [
(0,1,2,4,3,5),
(4,0,2,5,3,1),
(3,1,0,4,5,2),
(1,5,2,0,3,4),
(2,1,5,4,0,3),
(5,4,2,1,3,0)]

def zero_move_to_left(xi):
    r = roll[xi[0]]
    return [r[i] for i in xi]

def sort_middle_face(xi):
    m = xi[1:-1] 
    min_m = min(m)
    while m[0] != min_m:
        m = [m[1],m[3],m[0],m[2]]
    return xi[:1] + m + xi[-1:]

import sys
f = sys.stdin


str2i = {'Blue':0, 'Cyan':1, 'Green':2, 'Magenta':3, 'Red':4, 'Yellow':5}

import collections
while True:
    n = int(f.readline())
    if n == 0:
        break
    c = collections.Counter()
    for i in range(n):
        xi = [str2i[w] for w in f.readline().split()]
        xi = zero_move_to_left(xi)
        xi = sort_middle_face(xi)
        c[tuple(xi)] += 1
    print(n - len(c))

Python: roll = [
(0,1,2,3,4,5),
(1,5,2,3,0,4),
(2,1,5,0,4,3),
(3,1,0,5,4,2),
(4,0,2,3,5,1),
(5,4,2,3,1,0)]

def zero_move_to_left(xi):
    r = roll[xi.index(0)]
    return [xi[ri] for ri in r]

def sort_middle_face(xi):
    m = xi[1:-1] 
    min_m = min(m)
    while m[0] != min_m:
        m = [m[1],m[3],m[0],m[2]]
    return xi[:1] + m + xi[-1:]

import sys
f = sys.stdin


str2i = {'Blue':0, 'Cyan':1, 'Green':2, 'Magenta':3, 'Red':4, 'Yellow':5}

import collections
while True:
    n = int(f.readline())
    if n == 0:
        break
    c = collections.Counter()
    for i in range(n):
        xi = [str2i[w] for w in f.readline().split()]
        xi = zero_move_to_left(xi)
        xi = sort_middle_face(xi)
        c[tuple(xi)] += 1
    print(n - len(c))