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>
#include<math.h>
int main(void)
{
	int a,s[31][7],d[31],f[31],g,h,i,j,k,l,o,x;
	char z[31][7][10];
	while(1){
		scanf("%d",&a);
		if(a==0) break;
		for(i=0;i<a;i++){
			for(j=0;j<6;j++){
				scanf("%s",z[i][j]);
				if(z[i][j][0]=='R') s[i][j]=0;
				if(z[i][j][0]=='Y') s[i][j]=1;
				if(z[i][j][0]=='B') s[i][j]=2;
				if(z[i][j][0]=='M') s[i][j]=3;
				if(z[i][j][0]=='G') s[i][j]=4;
				if(z[i][j][0]=='C') s[i][j]=5;
			}
		}
		for(i=0;i<a;i++) f[i]=0;
		g=0;
		for(i=0;i<a-1;i++){
			if(f[i]==0){
				for(j=i+1;j<a;j++){
					for(k=0;k<5;k++){
						h=s[j][1];
						s[j][1]=s[j][0];
						s[j][0]=s[j][4];
						s[j][4]=s[j][5];
						s[j][5]=h;
						//printf("%d\n",s[j][5]);
						for(l=0;l<4;l++){
							x=0;
							h=s[j][1];
							s[j][1]=s[j][3];
							s[j][3]=s[j][4];
							s[j][4]=s[j][2];
							s[j][2]=h;
							for(o=0;o<6;o++){
								if(s[i][o]!=s[j][o]) break;
								else x++;
							}
							if(x==6){
								f[j]=1;
								g++;
								break;
							}
						}
						if(f[j]==1) break;
					}
					if(f[j]==0){
						h=s[j][2];
						s[j][2]=s[j][5];
						s[j][5]=s[j][3];
						s[j][3]=s[j][0];
						s[j][0]=h;
						for(l=0;l<4;l++){
							x=0;
							h=s[j][1];
							s[j][1]=s[j][3];
							s[j][3]=s[j][4];
							s[j][4]=s[j][2];
							s[j][2]=h;
							for(o=0;o<6;o++){
								if(s[i][o]!=s[j][o]) break;
								else x++;
							}
						}
						h=s[j][2];
						s[j][2]=s[j][0];
						s[j][0]=s[j][3];
						s[j][3]=s[j][5];
						s[j][5]=h;
						if(x==6){
							f[j]=1;
							g++;
						}
						if(f[j]==0){
							h=s[j][3];
							s[j][3]=s[j][5];
							s[j][5]=s[j][2];
							s[j][2]=s[j][0];
							s[j][0]=h;
							//printf("%d\n",s[j][5]);
							for(l=0;l<4;l++){
							x=0;
							h=s[j][1];
							s[j][1]=s[j][3];
							s[j][3]=s[j][4];
							s[j][4]=s[j][2];
							s[j][2]=h;
								for(o=0;o<6;o++){
									if(s[i][o]!=s[j][o]) break;
									else x++;
								}
							}
							if(x==6){
								f[j]=1;
								g++;
							}
							h=s[j][2];
							s[j][2]=s[j][5];
							s[j][5]=s[j][3];
							s[j][3]=s[j][0];
							s[j][0]=h;
						}
					}
				}
			}
		}
		printf("%d\n",g);
	}
	return 0;
}

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

struct BOX {
  string top;
  string back, left, right, head;
  string buttom;
  bool operator==(const BOX& b) const {
    return(top    == b.top   &&
           back   == b.back  &&
           left   == b.left  &&
           right  == b.right &&
           head   == b.head  &&
           buttom == b.buttom);
  }
};
class UnionFind{
private:
  vector< int > rank, p;
  void link(int x,int y){
    if(rank[x] > rank[y]){
      p[y] = x;
    }else{
      p[x] = y;
      if(rank[x] == rank[y]) rank[y]++;
    }
  }
public:
  UnionFind(int size){
    rank.resize(size,0);
    p.resize(size,0);
    for(int i = 0; i < rank.size(); i++){
      p[i] = i, rank[i] = 0;
    }
  }
  void Unite(int x,int y){
    if(Find(x) != Find(y)) link(Find(x),Find(y));
  }
  int Find(int x){
    return( x != p[x] ? p[x] = Find(p[x]) : p[x]);
  }
};


void Roll(BOX& B)
{
  string temp = B.head;
  B.head = B.right;
  B.right = B.back;
  B.back = B.left;
  B.left = temp;
}

void RoLL(BOX& B) {
  string temp = B.head;
  B.head = B.top;
  B.top = B.back;
  B.back = B.buttom;
  B.buttom = temp;
}

bool Myon(BOX& a, BOX& b)
{
  for(int k = 0; k < 4; k++) {
    Roll(a);
    for(int l = 0; l < 4; l++) {
      RoLL(a);
      if(a == b) return(true); 
    }
  }
  return(false);
}

int main()
{
  int n;
  vector< BOX > boxes;
  while(cin >> n, n) {
    UnionFind uf(n);

    boxes.resize(n);
    int ret = 0;

    for(int i = 0; i < n; i++) {
      BOX &b = boxes[i];
      cin >> b.top >> b.head >> b.right >> b.back >> b.left >> b.buttom;
    }
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(Myon(boxes[i], boxes[j])) uf.Unite(i, j);
      }
    }
    for(int i = 0; i < n; i++) {
      ret += uf.Find(i) == i;
    }
    cout << n - ret << endl;
  }
}

C++: #include<iostream>
using namespace std;
int box[30][6];
bool turn(int x,int change[6],int a,int b,int c,int d){
  int temp;
  //手前
  for(int i=0;i<a;i++){
    temp=change[0];
    change[0]=change[4];
    change[4]=change[5];
    change[5]=change[1];
    change[1]=temp;
  }
  for(int i=0;i<b;i++){
    temp=change[0];
    change[0]=change[1];
    change[1]=change[5];
    change[5]=change[4];
    change[4]=temp;
  }
  for(int i=0;i<c;i++){
    temp=change[0];
    change[0]=change[3];
    change[3]=change[5];
    change[5]=change[2];
    change[2]=temp;
  }
  for(int i=0;i<d;i++){
    temp=change[0];
    change[0]=change[2];
    change[2]=change[5];
    change[5]=change[3];
    change[3]=temp;
  }
  if(change[0]==box[x][0] && change[1]==box[x][1] && change[2]==box[x][2] && change[3]==box[x][3] && change[4]==box[x][4] && change[5]==box[x][5]){
    return true;
  }
  else return false;
}
int main(){
  int n;
  while(cin>> n,n){
    int ans=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<6;j++){
	string s;
	cin>> s;
	if(s=="Red") box[i][j]=0;
	else if(s=="Yellow")  box[i][j]=1;
	else if(s=="Blue")    box[i][j]=2;
	else if(s=="Magenta") box[i][j]=3;
	else if(s=="Green")   box[i][j]=4;
	else if(s=="Cyan")    box[i][j]=5;
      }
    }
    
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	if(i==j) continue;
	int change[6];
	change[0]=box[j][0]; change[1]=box[j][1];
	change[2]=box[j][2]; change[3]=box[j][3];
	change[4]=box[j][4]; change[5]=box[j][5];
	for(int a=0;a<4;a++){
	  for(int b=0;b<4;b++){
	    for(int c=0;c<4;c++){
	      for(int d=0;d<4;d++){
		if(turn(i,change,a,b,c,d)){
		  ans++;
		  a=4;b=4;c=4;d=4;
		}
	      }
	    }
	  }
	}
      }
    }
    cout<< ans/2<< endl;
  }
}