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
}

C++: #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main(void)
{
	int n,f[30],i,j,k,x,cnt,l,flg;
	char pat1[7],pat2[7];
	char a[30][6],buf[10],wk;
	while(1)	{
		scanf(" %d",&n);
		if(n==0)	break;
		for(i=0;i<n;i++)	{
			for(j=0;j<6;j++)	{
				scanf("%s",buf);
				a[i][j]=buf[0];
			}
		}
//		for(i=0;i<n;i++)	{
//			for(j=0;j<6;j++)	printf("%c",a[i][j]);
//			printf("\n");
//		}
		pat1[6]='\0';	pat2[6]='\0';
		for(i=0;i<n;i++)	f[i]=0;
		for(i=0;i<n-1;i++)	{
			for(j=i+1;j<n;j++)	{
				for(k=0;k<6;k++)	{
					pat1[k]=a[i][k];
					pat2[k]=a[j][k];
				}
				flg=0;
				for(k=0;k<4;k++)	{
					wk=pat1[0];
					pat1[0]=pat1[2];
					pat1[2]=pat1[5];
					pat1[5]=pat1[3];
					pat1[3]=wk;
					for(l=0;l<4;l++)	{
						wk=pat1[1];
						pat1[1]=pat1[3];
						pat1[3]=pat1[4];
						pat1[4]=pat1[2];
						pat1[2]=wk;
						if(strcmp(pat1,pat2)==0)	{
							flg=1;
						}
					}
				}
				for(k=0;k<4;k++)	{
					wk=pat1[0];
					pat1[0]=pat1[1];
					pat1[1]=pat1[5];
					pat1[5]=pat1[4];
					pat1[4]=wk;
					for(l=0;l<4;l++)	{
						wk=pat1[1];
						pat1[1]=pat1[3];
						pat1[3]=pat1[4];
						pat1[4]=pat1[2];
						pat1[2]=wk;
						if(strcmp(pat1,pat2)==0)	{
							flg=1;	
						}
					}
				}
				if(flg==1)	{
					f[j]=1;
				}
			}
		}
		cnt=0;
		for(i=0;i<n;i++)	cnt+=f[i];
		printf("%d\n",cnt);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

typedef vector<int> vi;

inline vi rotate(const vi &t)
{
    int idx=0;
    while(idx<6 && t[idx]!=0) ++idx;

    vi a;
    if(idx==0) a=vi({0,1,2,3,4,5});
    else if(idx==1) a=vi({1,6,2,3,0,4});
    else if(idx==2) a=vi({2,1,5,0,4,3});
    else if(idx==3) a=vi({3,1,0,5,4,2});
    else if(idx==4) a=vi({4,0,2,3,5,1});
    else if(idx==5) a=vi({5,4,2,3,1,0});

    vi ret(6);
    rep(i,6) ret[i]=t[a[i]];
    return ret;
}

inline vi normalize(const vi &t)
{
    vi side({t[1],t[2],t[4],t[3]});

    vector<vi> s;
    rep(i,4)
    {
        vi a(4);
        rep(j,4) a[j]=side[(j+i)%4];
        s.pb(a);
    }
    sort(all(s));
    return s[0];
}

int main()
{
    int n;
    while(cin >>n,n)
    {
        vector<vi> d(n);
        rep(i,n)
        {
            vi t(6);
            rep(j,6)
            {
                string s;
                cin >>s;
                if(s=="Red") t[j]=0;
                else if(s=="Yellow") t[j]=1;
                else if(s=="Blue") t[j]=2;
                else if(s=="Magenta") t[j]=3;
                else if(s=="Green") t[j]=4;
                else if(s=="Cyan") t[j]=5;
            }
            d[i]=rotate(t);
        }

        set<vi> s[6];
        rep(i,n) s[d[i][5]].insert(normalize(d[i]));

        int diff=0;
        rep(i,6) diff+=s[i].size();
        cout << n-diff << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int H[6][6] = {
	{1,5,2,3,0,4}, // North : 奥へ移動   ( y:-1 )
	{3,1,0,5,4,2}, // East  : 右へ移動   ( x:+1 )
	{2,1,5,0,4,3}, // West  : 左へ移動   ( x:-1 )
	{4,0,2,3,5,1}, // South : 手前へ移動 ( y:+1 )
	{0,2,4,1,3,5}, // Right : 右回りに回転 (移動なし)
	{0,3,1,4,2,5}  // Left  : 左回りに回転 (移動なし)
};

// サイコロライブラリ
// d[0] := top,   d[1] := front 
// d[2] := right, d[3] := left
// d[4] := back,  d[5] := bottom
enum{TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM};
struct Cube{
	vector<string> d;
	// コンストラクタで初期化
	Cube(vector<string> v){
		if( v.size() == 6 ){
			d = v;
		}else{
			d = vector<string>(6);
		}
	}
	Cube(){ d = vector<string>(6); }
	// dirの方向に回転 (副作用なし)
	Cube roll(int dir){
		Cube result = (*this);
		vector<string> d_(6);
		for(int i = 0 ; i < 6 ; i++ ){
			d_[i] = d[ H[dir][i] ];
		}
		result.d = d_;
		return result;
	}
	void debug(){
		for(int i=0 ; i < 6 ; i++ ){
			cout << (*this).d[i] << " ";
		}
		cout << endl;
	}
	bool operator==(const Cube& c){
		for(int i=0 ; i < 6 ; i++ ){
			if( c.d[i] != (*this).d[i] )
				return false;
		}
		return true;
	}
	bool is_same(Cube c){
		Cube c_[24];
		c_[0] = c;
		c_[1] = c.roll(0);
		c_[2] = c.roll(1);
		c_[3] = c.roll(2);
		c_[4] = c.roll(3);
		c_[5] = (c.roll(0)).roll(0);
		
		for(int i=6 ; i < 12 ; i++ ){
			c_[i] = c_[i-6].roll(4);
		}
		for(int i=12 ; i < 18 ; i++ ){
			c_[i] = c_[i-6].roll(4);
		}
		for(int i=18 ; i < 24 ; i++ ){
			c_[i] = c_[i-6].roll(4);
		}
		
		for(int i=0 ; i < 24 ; i++ ){
			if( c_[i] == (*this) )
				return true;
		}
		return false;
	}
};

int main(){
	int n;
	while( cin >> n , n ){
		vector<Cube> v;
		 
		for(int i=0 ; i < n ; i++ ){
			Cube c;
			for(int j=0 ; j < 6 ; j++ ){
				string s;
				cin >> s;
				c.d[j] = s;
			}
			bool flag = true;
			for(int j=0 ; j < v.size() ; j++ ){
				if( c.is_same( v[j] ) ) flag = false;
			}
			
			if( flag ){
				v.push_back( c );
			}
		}
		cout << (n - v.size()) << endl;
	}
}