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 <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
  int i,j,k,l,m,o,n,a[30][7],ans,c;
  string str;
  bool f;
  while(1){
    cin >> n;
    if(n == 0) break;

    for(int t=0;t<n;t++){
      for(i=1;i<7;i++){
	cin >> str;
	if(str == "Cyan") a[t][i] = 1;
	else if(str == "Yellow") a[t][i] = 2;
	else if(str == "Red") a[t][i] = 3;
	else if(str == "Magenta") a[t][i] = 4;
	else if(str == "Green") a[t][i] = 5;
	else a[t][i] = 6;
      }
    }
    ans = 0;

    for(i=0;i<n-1;i++){
      for(j=i+1;j<n;j++){
	f = false;
	for(k=1;k<7;k++){
	  for(l=1;l<7;l++){
	    if(a[i][k] == a[j][l] /*&& a[i][7-k] == a[j][7-l]*/){
	      f = true;
	      int c1[4],c2[4];
	      if(k == 1){
		c1[0] = 5;
		c1[1] = 3;
		c1[2] = 2;
		c1[3] = 4;
	      }
	      if(l == 1){
		c2[0] = 5;
		c2[1] = 3;
		c2[2] = 2;
		c2[3] = 4;
	      }
	      if(k == 2){
		c1[0] = 1;
		c1[1] = 3;
		c1[2] = 5;
		c1[3] = 4;
	      }
	      if(l == 2){
		c2[0] = 1;
		c2[1] = 3;
		c2[2] = 5;
		c2[3] = 4;
	      }
	      if(k == 3){
		c1[0] = 5;
		c1[1] = 6;
		c1[2] = 2;
		c1[3] = 1;
	      }
	      if(l == 3){
		c2[0] = 5;
		c2[1] = 6;
		c2[2] = 2;
		c2[3] = 1;
	      }
	      if(k == 4){
		c1[0] = 5;
		c1[1] = 1;
		c1[2] = 2;
		c1[3] = 6;
	      }
	      if(l == 4){
		c2[0] = 5;
		c2[1] = 1;
		c2[2] = 2;
		c2[3] = 6;
	      }
	      if(k == 5){
		c1[0] = 6;
		c1[1] = 3;
		c1[2] = 1;
		c1[3] = 4;
	      }
	      if(l == 5){
		c2[0] = 6;
		c2[1] = 3;
		c2[2] = 1;
		c2[3] = 4;
	      }
	      if(k == 6){
		c1[0] = 5;
		c1[1] = 4;
		c1[2] = 2;
		c1[3] = 3;
	      }
	      if(l == 6){
		c2[0] = 5;
		c2[1] = 4;
		c2[2] = 2;
		c2[3] = 3;
	      }

	      for(m=0;m<4;m++){
		c = 0;
		for(o=0;o<4;o++){
		  if(a[i][c1[o]] == a[i][c2[(o+m)%4]]) c++;
		}
		if(c == 4){
		  ans++;
		  break;
		}
	      }
	    }
	    if(f) break;
	  }
	  if(f) break;
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}

C++: // 2011/08/01 Tazoe

#include <iostream>
#include <string>
using namespace std;

void roll01(int c[6])
{
	int tmp = c[0];
	c[0] = c[1];
	c[1] = c[5];
	c[5] = c[4];
	c[4] = tmp;
}

void roll02(int c[6])
{
	int tmp = c[0];
	c[0] = c[2];
	c[2] = c[5];
	c[5] = c[3];
	c[3] = tmp;
}

void roll12(int c[6])
{
	int tmp = c[1];
	c[1] = c[2];
	c[2] = c[4];
	c[4] = c[3];
	c[3] = tmp;
}

void rotate(int c[6])
{
	if(c[0]==1){
		;
	}
	else if(c[1]==1){
		roll01(c);
	}
	else if(c[2]==1){
		roll02(c);
	}
	else if(c[3]==1){
		roll02(c);
		roll02(c);
		roll02(c);
	}
	else if(c[4]==1){
		roll01(c);
		roll01(c);
		roll01(c);
	}
	else if(c[5]==1){
		roll02(c);
		roll02(c);
	}

	if(c[1]==2){
		;
	}
	else if(c[2]==2){
		roll12(c);
	}
	else if(c[4]==2){
		roll12(c);
		roll12(c);
	}
	else if(c[3]==2){
		roll12(c);
		roll12(c);
		roll12(c);
	}
	else if(c[1]==3){
		;
	}
	else if(c[2]==3){
		roll12(c);
	}
	else if(c[4]==3){
		roll12(c);
		roll12(c);
	}
	else if(c[3]==3){
		roll12(c);
		roll12(c);
		roll12(c);
	}
}

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int C[30][6];
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++){
				string str;
				cin >> str;
				if(str=="Red")
					C[i][j] = 1;
				else if(str=="Yellow")
					C[i][j] = 2;
				else if(str=="Blue")
					C[i][j] = 3;
				else if(str=="Magenta")
					C[i][j] = 4;
				else if(str=="Green")
					C[i][j] = 5;
				else if(str=="Cyan")
					C[i][j] = 6;
			}
		}

		for(int i=0; i<n; i++)
			rotate(C[i]);

		for(int i=0; i<n-1; i++)
			for(int j=i+1; j<n; j++)
				if(C[i][1]>C[j][1]
				 ||(C[i][1]==C[j][1]&&C[i][2]>C[j][2])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]>C[j][3])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]==C[j][3]&&C[i][4]>C[j][4])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]==C[j][3]&&C[i][4]==C[j][4]&&C[i][5]>C[j][5]))
					for(int k=0; k<6; k++){
						int tmp = C[i][k];
						C[i][k] = C[j][k];
						C[j][k] = tmp;
					}

		int m = n;
		for(int i=0; i<n-1; i++){
			bool flg = true;
			for(int j=0; j<6; j++)
				if(C[i][j]!=C[i+1][j]){
					flg = false;
					break;
				}
			if(flg)
				m--;
		}
		cout << n-m << endl;
	}

	return 0;
}