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 <bits/stdc++.h>
using namespace std;

struct Cube {
  string label[6];
  void swap(int a, int b, int c, int d) {
    string temp = label[a];
    label[a] = label[b];
    label[b] = label[c];
    label[c] = label[d];
    label[d] = temp;
  }
  void roll(char dir) {
    switch (dir) {
    case 'N': swap(0, 1, 5, 4); break;
    case 'E': swap(0, 3, 5, 2); break;
    case 'R': swap(1, 2, 4, 3); break;
    }
  }
  bool is_same(Cube& c) {
    for (auto& dir : "RRRRNRRRRERRRRNRRRRERRRRNRRRR") {
      if (sequence_equal(c)) return true;
      roll(dir);
    }
    return false;
  }
  bool sequence_equal(Cube& c) {
    for (int i = 0; i < 6; i++) {
      if (label[i] != c.label[i]) return false;
    }
    return true;
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<Cube> c(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 6; j++) {
        cin >> c[i].label[j];
      }
    }
    vector<int> m(n, 1);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (!m[i]) continue;
      for (int j = i + 1; j < n; j++) {
        if (m[j] && c[i].is_same(c[j])) {
          m[j] = 0;
          cnt++;
        }
      }
    }
    cout << cnt << "\n";
  }
}

C++: #include<bits/stdc++.h>
#include<set>
using namespace std;
#define pb push_back
int N;
int co(int i,int j,int k){
	if(i>j)swap(i,j);
	if(i>k)swap(i,k);
	if(j>k)swap(j,k);
	return i*36+j*6+k;
}
int co2(int i,int j){
	if(i>j)swap(i,j);
	return i*6+j;
}
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0)return 0;
		int X[6];
		set<vector<int> >S;
		for(int i=0;i<N;i++){
			for(int j=0;j<6;j++){
				string SS;
				cin >> SS;
				if(SS=="Cyan")X[j]=0;
				if(SS=="Yellow")X[j]=1;
				if(SS=="Red")X[j]=2;
				if(SS=="Magenta")X[j]=3;
				if(SS=="Green")X[j]=4;
				if(SS=="Blue")X[j]=5;
			}
			vector<int>XX;
			XX.pb(co2(X[0],X[5]));
			XX.pb(co2(X[1],X[4]));
			XX.pb(co2(X[2],X[3]));
			XX.pb(co(X[0],X[1],X[2]));
			XX.pb(co(X[1],X[2],X[5]));
			XX.pb(co(X[0],X[1],X[3]));
			XX.pb(co(X[1],X[3],X[5]));
			XX.pb(co(X[0],X[4],X[2]));
			XX.pb(co(X[4],X[2],X[5]));
			XX.pb(co(X[0],X[4],X[3]));
			XX.pb(co(X[4],X[3],X[5]));
			sort(XX.begin(),XX.end());
			S.insert(XX);
		}
		printf("%d\n",N-(int)S.size());
	}
}

C++: #include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef pair<P,P> Edge;
typedef long long ll;

//ifstream ifs("input.txt");
//#define cin ifs

// 24ñÅ³ÉàÇé
class Dice{
private:
  int yoko;
  int tate;
  int round;

private:
    int face[3][4];

public:
  int getFace(const int idx)const{
    if(idx==0)
      return face[1][1];
    else if(idx==1)
      return face[2][1];
    else if(idx==2)
      return face[1][2];
    else if(idx==3)
      return face[1][0];
    else if(idx==4)
      return face[0][1];
    else if(idx==5)
      return face[1][3];
    return -1;
  }
  Dice(const vector<int> &v){
    init();
    face[1][1]=v[0];
    face[2][1]=v[1];
    face[1][2]=v[2];
    face[1][0]=v[3];
    face[0][1]=v[4];
    face[1][3]=v[5];
  }
private:
  // _CXÌú»
  void init(){
    yoko=0;
    tate=0;
    round=0;
  }
public:
  // _CXÌñ]
  void rotate(){
    if(round<4){
    	int tmp = face[2][1];
    	face[2][1]=face[1][2];
    	face[1][2]=face[0][1];
    	face[0][1]=face[1][0];
    	face[1][0]=tmp;
    	round++;
    }
    if(round>=4){
    	round=0;
    	// àÇÁÄ«½çA¡ÌXbgðñ]³¹é
    	if(yoko<4){
    		int tmp = face[1][3];
    		for(int i = 2; i >= 0; i--){
    			face[1][i+1]=face[1][i];
    		}
    		face[1][0]=tmp;
    		yoko++;
    	}
    	// àÇÁÄ«½çAcÌXbgðñ]³¹é
    	if(yoko>=4){
    		if(tate<4){
    			tate++;
    			int tmp = face[1][3];
    			face[1][3]=face[0][1];
    			for(int i = 0; i < 2; i++){
    				face[i][1]=face[i+1][1];
    			}
    			face[2][1]=tmp;
    		}
    		if(tate>=4){
    			round=0;
    			tate=0;
    			yoko=0;
    		}
    	}
    }
  };
};

int main(){
	int n;
	while(cin>>n&&n!=0){
		map<string,int> dict;
		dict["Cyan"]=0;
		dict["Yellow"]=1;
		dict["Red"]=2;
		dict["Magenta"]=3;
		dict["Green"]=4;
		dict["Blue"]=5;
		vector<Dice> vd;
		for(int i=0;i<n;i++){
			vector<int> vs;
			for(int j=0;j<6;j++){
				string s;
				cin>>s;
				vs.push_back(dict[s]);
			}
			vd.push_back(Dice(vs));
		}
		int cnt=0;
		// ¡ÜÅÉo»µ½p^[ðo^
		set<vector<int> > svs;
		for(int i=0;i<n;i++){
			Dice &dice=vd[i];
			vector<int> vs2;
			// ¡ñÌÊðÂ­é
			for(int k=0;k<6;k++)
				vs2.push_back(dice.getFace(k));
			// ±ÌÊª¡ÜÅo»µÄ¢êÎAcontinue
			if(svs.find(vs2)!=svs.end())continue;
			// »¤ÅÈ¯êÎAJEgðâ·
			cnt++;
			// ¡ñÌ_CXÌSu«ûðo^
			for(int j=0;j<24;j++){
				vector<int> vs;
				for(int k=0;k<6;k++)
					vs.push_back(dice.getFace(k));
				// u«ûðo^
				svs.insert(vs);
				// _CXðñ]
				dice.rotate();
			}
		}
		// o»µ½p^[ÌðAn©çø­
		cout<<n-cnt<<endl;
	}
	return 0;
}