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++: #ifndef ___Rank_Union_Find
#define ___Rank_Union_Find

#include <vector>

// ------ Class ------ //
class UnionFind {
private:
	unsigned size_; std::vector<unsigned> par, rank;
public:
	UnionFind() : size_(0), par(std::vector<unsigned>()), rank(std::vector<unsigned>()) {};
	UnionFind(unsigned size__) : size_(size__) {
		par.resize(size_); rank.resize(size_);
		for (unsigned i = 0; i < size_; i++) par[i] = i, rank[i] = 0;
	}
	unsigned size() { return size_; }
	unsigned root(unsigned x) { return par[x] == x ? x : par[x] = root(par[x]); }
	bool same(unsigned x, unsigned y) { return root(x) == root(y); }
	void unite(unsigned x, unsigned y) {
		x = root(x), y = root(y);
		if (x == y) return;
		if (rank[x] < rank[y]) par[x] = y;
		else if (rank[x] == rank[y]) par[y] = x, rank[x]++;
		else par[y] = x;
	}
	bool operator==(const UnionFind &u) { return par == u.par; }
	bool operator!=(const UnionFind &u) { return par != u.par; }
};

#endif

#include <set>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> x, y; int t;
bool same(vector<int> a, vector<int> b) {
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 4; k++) {
				for (int l = 0; l < 4; l++) {
					string s;
					for (int m = 0; m < i; m++) s += 'N';
					for (int m = 0; m < j; m++) s += 'E';
					for (int m = 0; m < k; m++) s += 'S';
					for (int m = 0; m < l; m++) s += 'W';
					for (int i = 0; i < s.size(); i++) {
						t = a[0];
						switch (s[i]) {
						case 'N':
							a[0] = a[1];
							a[1] = a[5];
							a[5] = a[4];
							a[4] = t;
							break;
						case 'E':
							a[0] = a[3];
							a[3] = a[5];
							a[5] = a[2];
							a[2] = t;
							break;
						case 'S':
							a[0] = a[4];
							a[4] = a[5];
							a[5] = a[1];
							a[1] = t;
							break;
						case 'W':
							a[0] = a[2];
							a[2] = a[5];
							a[5] = a[3];
							a[3] = t;
							break;
						}
					}
					if (a == b) return true;
				}
			}
		}
	}
	return false;
}
int n; string s;
int main() {
	while (cin >> n, n) {
		vector<vector<int> > v(n, vector<int>(6));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 6; j++) {
				cin >> s;
				v[i][j] = s[0];
			}
		}
		UnionFind uf(n);
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (same(v[i], v[j])) uf.unite(i, j);
			}
		}
		set<int> s_;
		for (int i = 0; i < n; i++) s_.insert(uf.root(i));
		cout << n - s_.size() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
char cube[30][6];

int main()
{
	int n;
	while(cin >> n){
		if(n==0)
			break;
		string str;
		for(int i=0;i<n;i++){
			for(int j=0;j<6;j++){
				cin >> str;
				cube[i][j] = str[0];
			}
		}
		int count = 0;
		for(int i=0;i<n-1;i++){
			bool flag=false,flug=false;
			string so = "";
			so += cube[i][3];
			so += cube[i][0];
			so += cube[i][2];
			so += cube[i][5];
			for(int j=i+1;j<n;j++){
				//2,5ツ板サツ津ィ
				if((cube[i][1] == cube[j][1] && cube[i][4] == cube[j][4]) || (cube[i][1] == cube[j][4] && cube[i][4] == cube[j][1]))
					flag = true;
				//ツ残ツづィツ板サツ津ィ
				string ds = "";
				ds += cube[j][3];
				ds += cube[j][0];
				ds += cube[j][2];
				ds += cube[j][5];
				ds += ds;
				string dds = "";
				dds += cube[j][5];
				dds += cube[j][2];
				dds += cube[j][0];
				dds += cube[j][3];
				dds += dds;
				for(int i=0;i<8;i++){
					if(so[0] == ds[i]){
						flug = true;
						for(int j=0;j<4;j++){
							if(so[j] != ds[i+j])
								flug = false;
						}
						if(flug)
							break;
					}
					if(so[0] == dds[i]){
						flug = true;
						for(int j=0;j<4;j++){
							if(so[j] != dds[i+j])
								flug = false;
						}
						if(flug)
							break;
					}
				}
				if(flag && flug){
					count++;
					break;
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}