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

int rot[24][6] = {
    {0,1,2,3,4,5}, {0,2,4,1,3,5}, {0,4,3,2,1,5}, {0,3,1,4,2,5},
    {1,5,2,3,0,4}, {1,2,0,5,3,4}, {1,0,3,2,5,4}, {1,3,5,0,2,4},
    {2,5,4,1,0,3}, {2,4,0,5,1,3}, {2,0,1,4,5,3}, {2,1,5,0,4,3},
    {3,4,5,0,1,2}, {3,5,1,4,0,2}, {3,1,0,5,4,2}, {3,0,4,1,5,2},
    {4,0,2,3,5,1}, {4,2,5,0,3,1}, {4,5,3,2,0,1}, {4,3,0,5,2,1},
    {5,4,2,3,1,0}, {5,2,1,4,3,0}, {5,1,3,2,4,0}, {5,3,4,1,2,0}
};

int main() {
    int n;
    while (cin >> n, n) {
        vector<vector<string> > work;
        for (int i=0; i<n; ++i) {
            vector<string> v(6);
            for (int j=0; j<6; ++j) {
                cin >> v[j];
            }
            work.push_back(v);
        }
        int res = 0;
        for (int i=0; i<n; ++i) {
            bool ok = true;
            for (int j=i+1; j<n && ok; ++j) {
                for (int k=0; k<24 && ok; ++k) {
                    if (work[i][0] == work[j][rot[k][0]] &&
                        work[i][1] == work[j][rot[k][1]] &&
                        work[i][2] == work[j][rot[k][2]] &&
                        work[i][3] == work[j][rot[k][3]] &&
                        work[i][4] == work[j][rot[k][4]] &&
                        work[i][5] == work[j][rot[k][5]]) {
                        ok = false;
                    }
                }
            }
            if ( ! ok) ++res;
        }
        cout << res << endl;
    }
    return 0;
}

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;
    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;
    }
  }
  
  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;
    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;
    }
  }
  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;
    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;
    }
  }
  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;
    }
  }
  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;
  }
  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;
  }
}