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<stdio.h>
#include<ctype.h>
#include<string>
#include<cmath>
#include<math.h>
#include<vector>
#include<stdlib.h>
#include<algorithm>
#include<functional>

using namespace std;

#define OUT cout <<
#define IN cin >>
#define E << endl;
#define FOR(i,a,b) for(int i = a;i < b;i++)

int n;

void input(){
	IN n;
}

class Dice{
	private:
		string temp;
	public:
		string c[6];
		Dice(){}
		void roll(int axis){
			if(axis == 1){
				temp = c[1];
				c[1] = c[2];
				c[2] = c[6];
				c[6] = c[5];
				c[5] = temp;
			}
			else{
				temp = c[2];
				c[2] = c[4];
				c[4] = c[5];
				c[5] = c[3];
				c[3] = temp;
			}
		}
		void getd(string& c1, string& c2, string& c3, string& c4, string& c5, string& c6){
			c1 = c[0];
			c2 = c[1];
			c3 = c[2];
			c4 = c[3];
			c5 = c[4];
			c6 = c[5];
		}

		void input(){
			FOR(i,0,6){
				IN c[i];
			}
		}

};

int main(){
	while(true){
		input();
		if(n == 0)break;
		int count = 0;
		bool match[n];
		FOR(i, 0, n){
			match[i] = false;
		}
		string str1[6];
		string str2[6];	

		vector<Dice> dice;
	
		dice.resize(n);
		FOR(i, 0, n){
			dice[i].input();
		}
			

		FOR(i, 0, n - 1){
			FOR(j, i + 1, n){
				bool flag2 = false;
				FOR(x, 0, 4){
					FOR(y, 0, 4){
						dice[i].getd(str1[0],str1[1],str1[2],str1[3],str1[4],str1[5]);
						dice[j].getd(str2[0],str2[1],str2[2], str2[3],str2[4],str2[5]);
						
						bool flag1 = true;
						FOR(d, 0, 6){					
							if(str1[d] != str2[d]){
								flag1 == false;
							}
						}
						if(flag1){
							if(match[i] == false){							
								flag2 = true;
							}
							match[j] = true;
						}
						flag1 = true;
					
						dice[i].roll(y);
					}
					dice[i].roll(x);
				}
				OUT i << " " << j << " " << flag2 E
				if(flag2){
					count++;
					OUT "count++ " << i << " " << j  E
				}
			}
		}	

		OUT count E

	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int trans[6][6] =
{
	{1, 3, 4, 5, 2, 6},
	{5, 1, 3, 4, 6, 2},
	{4, 2, 1, 6, 5, 3},
	{3, 2, 6, 1, 5, 4},
	{2, 6, 3, 4, 1, 5},
	{6, 5, 3, 4, 2, 1}	
};

struct Cube
{
	vector<string> colors;
	Cube() : colors(6) {}
	
	Cube rotate(int s)
	{
		vector<string> tmp(6);
		rep(i, 6) tmp[trans[s][i]-1] = colors[i];
		colors = tmp;
		
		return *this;
	}
};

bool same(Cube a, Cube b)
{
	rep(i, 6) if(a.colors[0] == b.colors[i]) b.rotate(i);
	
	rep(i, 4) if(a.colors == b.rotate(0).colors) return true;
	
	return false;
}

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
	int count() {
		int res = 0;
		rep(i, data.size()) res += data[i] < 0;
		return res;
	}
};

int main()
{
    int n;
	while(cin >> n && n)
	{
		int ans = 0;
		vector<Cube> cubes(n);
		
		rep(i, n) rep(j, 6) cin >> cubes[i].colors[j];
		
		UnionFind uft(n);
		rep(i, n) REP(j, i+1, n) uft.unite(i, j);
		
		cout << uft.count() << endl;
	}
}

C++: #include <iostream>
#include <string>
#include <string.h>
using namespace std;

class Dice
{
public:
	Dice( char c[ 6 ] )
	{
		for( int i = 0; i < 6; ++i ){
			face[ i ] = c[ i ];
		}
		face[ 6 ] = '\0';
	}
	void rotateVertical()
	{
		char t = face[ 0 ];
		face[ 0 ] = face[ 1 ];
		face[ 1 ] = face[ 5 ];
		face[ 5 ] = face[ 4 ];
		face[ 4 ] = t;
	}
	void rotateHorizontal()
	{
		char t = face[ 1 ];
		face[ 1 ] = face[ 2 ];
		face[ 2 ] = face[ 4 ];
		face[ 4 ] = face[ 3 ];
		face[ 3 ] = t;
	}
	char roundFace( int index )
	{
		static int a[] = { 1, 2, 4, 3 };
		return face[ a[ index ] ];
	}
	int getId()
	{
		static string pattern[] = {
			"RBMGCY", "RBMCGY", "RBGMCY", "RBGCMY", "RBCMGY", "RBCGMY",
			"RYMGCB", "RYMCGB", "RYGMCB", "RYGCMB", "RYCMGB", "RYCGMB",
			"RYBGCM", "RYBCGM", "RYGBCM", "RYGCBM", "RYCBGM", "RYCGBM",
			"RYBMCG", "RYBCMG", "RYMBCG", "RYMCBG", "RYCBMG", "RYCMBG",
			"RYBMGC", "RYBGMC", "RYMBGC", "RYMGBC", "RYGBMC", "RYGMBC",
		};
		if( face[ 2 ] == 'R' || face[ 3 ] == 'R' ){
			rotateHorizontal();
		}
		while( face[ 0 ] != 'R' ){
			rotateVertical();
		}
		if( face[ 5 ] == 'Y' ){
			while( face[ 1 ] != 'B' ){
				rotateHorizontal();
			}
		}else{
			while( face[ 1 ] != 'Y' ){
				rotateHorizontal();
			}
		}
		string diceStr( face );
		for( int i = 0; i < 30; ++i ){
			if( pattern[ i ] == diceStr ){
				return i;
			}
		}
		return -1;
	}

	char face[ 6 + 1 ];
};

int main()
{
	int n;
	while( cin >> n && n ){
		int id[ 30 ];
		memset( id, 0, sizeof(id) );

		for( int i = 0; i < n; ++i ){
			string s;
			char c[ 6 ];
			for( int i = 0; i < 6; ++i ){
				cin >> s;
				c[ i ] = s[ 0 ];
			}
			Dice dice( c );
			++id[ dice.getId() ];
		}

		int ans = 0;
		for( int i = 0; i < 30; ++i ){
			if( id[ i ] > 1 ){
				++ans;
			}
		}
		cout << ans << endl;
	}

	return 0;
}