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 <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

typedef vector <int> VI;

VI roll_y( VI& box )
{
	VI work = box;
	work[0] = box[2];
	work[2] = box[5];
	work[5] = box[3];
	work[3] = box[0];
	return work;
}

VI roll_x( VI& box )
{
	VI work = box;
	work[5] = box[1];
	work[1] = box[0];
	work[0] = box[4];
	work[4] = box[5];
	return work;
}

VI roll_z( VI& box )
{
	VI work = box;
	work[3] = box[4];
	work[4] = box[2];
	work[2] = box[1];
	work[1] = box[3];
	return work;
}

void register_box( VI& box, set <VI>& V )
{
	V.insert( box );

	VI work;
	
	work = box;
	for ( int i = 0; i < 3; i++ )
	{
		work = roll_y( work );
		V.insert( work );
	}

	work = box;
	for ( int j = 0; j < 3; j++ )
	{
		work = roll_x( work );
		V.insert( work );
		for ( int i = 0; i < 3; i++ )
		{
			work = roll_y( work );
			V.insert( work );
		}
		work = roll_y( work );
	}

	work = box;
	for ( int j = 0; j < 3; j++ )
	{
		work = roll_z( work );
		V.insert( work );
		for ( int i = 0; i < 3; i++ )
		{
			work = roll_y( work );
			V.insert( work );
		}
		work = roll_y( work );
	}

}

int main( void )
{
	// Redツ、Yellowツ、Blueツ、Magentaツ、Greenツ、Cyan
	map <string, int> hash_table;
	hash_table["Red"]		= 1;
	hash_table["Yellow"]	= 2;
	hash_table["Blue"]		= 3;
	hash_table["Magenta"]	= 4;
	hash_table["Green"]		= 5;
	hash_table["Cyan"]		= 6;

	int n;
	while ( cin >> n && n )
	{
		int cnt = 0;
		set < VI > V;
		VI box(6);
		string color;

		for ( int i = 0; i < n; i++ )
		{
			for ( int j = 0; j < 6; j++ )
			{
				cin >> color;
				box[j] = hash_table[color];
			}

			if ( V.find( box ) != V.end() )
				cnt ++;

			register_box( box, V );
		}

		cout << cnt << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;

int data[30][6];

bool judge(int);

main(){
  string in;
  int n;
  while(1){
    cin >> n;
    if(n==0) break;
    int count=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<6;j++){
	cin >> in;
	if(in=="Red") data[i][j]=1;
	if(in=="Yellow") data[i][j]=2;
	if(in=="Blue") data[i][j]=3;
	if(in=="Magenta") data[i][j]=4;
	if(in=="Green") data[i][j]=5;
	if(in=="Cyan") data[i][j]=6;
      }
      if(judge(i)) count++;
    }
    int ans=n-count;
    //    if(ans<0) ans=0;
    cout << ans << endl;
  }
  return 0;
}

bool judge(int n){
  if(n==0) return true;
  int tmp[6];
  for(int i=0;i<6;i++){
    tmp[i]=data[n][i];
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<4;j++){
      for(int k=0;k<4;k++){
	if(tmp[0]==data[i][0] && tmp[1]==data[i][1] && tmp[2]==data[i][2] && tmp[3]==data[i][3] && tmp[4]==data[i][4] && tmp[5]==data[i][5]) return false;
	int tt[6];
	for(int l=0;l<6;l++){
	  tt[l]=tmp[l];
	}
	tt[0]=tmp[1];
	tt[1]=tmp[5];
	tt[2]=tmp[2];
	tt[3]=tmp[3];
	tt[4]=tmp[0];
	tt[5]=tmp[4];
	for(int l=0;l<6;l++){
	  tmp[l]=tt[l];
	}
      }
      int t[6];
      for(int k=0;k<6;k++){
	t[k]=tmp[k];
      }
      t[0]=tmp[3];
      t[1]=tmp[1];
      t[2]=tmp[0];
      t[3]=tmp[5];
      t[4]=tmp[4];
      t[5]=tmp[2];
      for(int k=0;k<6;k++){
	tmp[k]=t[k];
      }
    }
  }
  return true;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

const int MAX_N = 30;

class UnionFind{
public:
    UnionFind(){}
    UnionFind(int n){
        REP(i, n){
            par[i] = i;
            rank[i] = 0;
        }
    }
    int find(int x){
        if(x == par[x])return x;
        return par[x] = find(par[x]);
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y){return;}
        
        if(rank[x] > rank[y]){
            par[y] = x;
        }else{
            par[x] = y;
            if(rank[x] == rank[y]){rank[y]++;}
        }
    }
private:
    int rank[MAX_N], par[MAX_N];
};

int N;
int dices[30][6];
std::string colors[6] = {"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan"};
const std::vector<int> route[] = {
    {0, 1, 5, 4},
    {1, 2, 4, 3}
}, remaining[] = {{2, 3}, {0, 5}};

bool same(int i, int j){
    // a: ルート, b: 始点, c: 方向
    REP(a, 2){
        REP(b, 4){
            REP(c, 2){
                REP(d, 2){
                    REP(e, 4){
                        REP(f, 2){
                            int x = b, y = e;
                            bool can = true;
                            REP(g, 4){
                                if(dices[i][route[a][x]] != dices[j][route[d][y]]){can = false; break;}
                                x = (x + dy[c] + 4) % 4; y = (y + dy[f] + 4) % 4;
                            }
                            if(!can){continue;}
                            if((dices[i][remaining[a][0]] == dices[j][remaining[d][0]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][1]]) ||
                               (dices[i][remaining[a][0]] == dices[j][remaining[d][1]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][0]])){
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

int main(){
    while(std::cin >> N, N){
        UnionFind uf(N);

        REP(i, N){
            REP(j, 6){
                std::string s;
                std::cin >> s;

                REP(k, 6){
                    if(s == colors[k]){dices[i][j] = k;}
                }
            }
        }

        REP(i, N){
            FOR(j, i+1, N){
                if(same(i, j)){
                    uf.unite(i, j);
                }
            }
        }

        int res = N;
        REP(i, N){
            if(i == uf.find(i)){res -= 1;}
        }

        std::cout << res << std::endl;
    }
}