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 <stdio.h>
#include <string.h>
 
int main()
{
    int i0, i1, i2, i3, n, m, d[30][6], s[30], sw;
    char c[8];
    char color[6][8] = {"Cyan", "Yellow", "Red", "Magenta", "Green", "Blue"};
 
    scanf("%d", &n);
    while (n > 0) {
        for (i0 = 0; i0 < n; i0++) {
            for (i1 = 0; i1 < 6; i1++) {
                scanf("%s", c);
                for (i2 = 0; i2 < 6; i2++) {
                    if (strcmp(c, color[i2]) == 0) {
                        d[i0][i1] = i2;
                        break;
                    }
                }
            }
        }
        m = 0;
        for (i0 = 0; i0 < n; i0++)
            s[i0] = 1;
        for (i0 = 0; i0 < n; i0++) {
            if (s[i0] > 0) {
                m++;
                s[i0] = 0;
                for (i1 = i0+1; i1 < n; i1++) {
                    sw = 0;
                    for (i2 = 0; i2 < 6 && sw == 0; i2++) {
                        for (i3 = 0; i3 < 6 && sw == 0; i3++) {
                            if (d[i0][i2] == d[i1][i3]) {
                                if (d[i0][5-i2] != d[i1][5-i3])
                                    sw = 1;
                            }
                        }
                    }
                    if (sw == 0)
                        s[i1] = 0;
                }
            }
        }
        printf("%d\n", n-m);
        scanf("%d", &n);
    }
 
    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
struct kyu_bu{
  string kyu[7];
  bool h;
};
kyu_bu p[30];
void North(int i){
  string mem;
  mem = p[i].kyu[1];
  p[i].kyu[1] = p[i].kyu[2];
  p[i].kyu[2] = p[i].kyu[6];
  p[i].kyu[6] = p[i].kyu[5];
  p[i].kyu[5] = mem;
}
void East(int i){
  string mem;
  mem = p[i].kyu[1];
  p[i].kyu[1] = p[i].kyu[4];
  p[i].kyu[4] = p[i].kyu[6];
  p[i].kyu[6] = p[i].kyu[3];
  p[i].kyu[3] = mem;
}
void Right(int i){
  string mem;
  mem = p[i].kyu[2];
  p[i].kyu[2] = p[i].kyu[3];
  p[i].kyu[3] = p[i].kyu[5];
  p[i].kyu[5] = p[i].kyu[4];
  p[i].kyu[4] = mem;
}
int main(){
  int n;
  int roop[7]={0,2,3,4,5,6,1};
  while(cin >> n){
    if(n==0) break;
    int count = 0;
    for(int i=0;i<n;i++){
      p[i].h = true;
      for(int j=1;j<=6;j++) cin >> p[i].kyu[j];
    }
    for(int i=0;i<n-1;i++){
      if(!p[i].h) continue;
      for(int j=0;j<=12;j++){
	if(j>=1&&j<=4) North(i);
	else if(j>=5&&j<=8) East(i);
	else if(j>=9&&j<=12)Right(i);
	for(int k=i+1;k<n;k++){
	  if(!p[k].h) continue;
	  for(int g=1;g<=6;g++){
	    if(p[i].kyu[1]==p[k].kyu[g]){
	      int strat = roop[g];
	      int cou = 1;
	      int index = 2;
	      while(cou!=6){
		if(p[i].kyu[index]!=p[k].kyu[strat]) break;
		cou++;
		index++;
		strat = roop[strat];   
	      }
	      if(cou==6){
		count++;
		p[k].h = false;
	      }
	    }
	  }
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}

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

int cc(int,int);

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];
	      for(m=0;m<4;m++){
		c1[m] = cc(k,m);
		c2[m] = cc(l,m);
	      }

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

    cout << ans << endl;

  }
  return 0;
}


int cc(int a,int b){

  if(a == 1){
    if(b == 0) return 5;
    if(b == 1) return 3;
    if(b == 2) return 2;
    if(b == 3) return 4;
  }
  if(a == 2){
    if(b == 0) return 1;
    if(b == 1) return 3;
    if(b == 2) return 5;
    if(b == 3) return 4;
  }
  
  if(a == 3){
    if(b == 0) return 5;
    if(b == 1) return 6;
    if(b == 2) return 2;
    if(b == 3) return 1;
  }
  
  if(a == 4){
    if(b == 0) return 5;
    if(b == 1) return 1;
    if(b == 2) return 2;
    if(b == 3) return 6;
  }
  
  if(a == 5){
    if(b == 0) return 6;
    if(b == 1) return 3;
    if(b == 2) return 1;
    if(b == 3) return 4;
  }
  
  if(a == 6){
    if(b == 0) return 5;
    if(b == 1) return 4;
    if(b == 2) return 2;
    if(b == 3) return 3;
  }
}