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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

const int MAX_N = 30;

class UnionFind{
public:
    UnionFind(){}
    UnionFind(int n){
        REP(i, n){
            par[i] = i;
            rank[i] = 0;
        }
    }
    int find(int x){
        if(x == par[x])return x;
        return par[x] = find(par[x]);
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y){return;}
        
        if(rank[x] > rank[y]){
            par[y] = x;
        }else{
            par[x] = y;
            if(rank[x] == rank[y]){rank[y]++;}
        }
    }
private:
    int rank[MAX_N], par[MAX_N];
};

int N;
int dices[30][6];
std::string colors[6] = {"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan"};
const std::vector<int> route[] = {
    {0, 1, 5, 4},
    {1, 2, 4, 3}
}, remaining[] = {{2, 3}, {0, 5}};

bool same(int i, int j){
    // a: ルート, b: 始点, c: 方向
    REP(a, 2){
        REP(b, 4){
            REP(c, 2){
                REP(d, 2){
                    REP(e, 4){
                        REP(f, 2){
                            int x = b, y = e;
                            bool can = true;
                            REP(g, 4){
                                if(dices[i][route[a][x]] != dices[j][route[d][y]]){can = false; break;}
                                x = (x + dy[c] + 4) % 4; y = (y + dy[f] + 4) % 4;
                            }
                            if(!can){continue;}
                            if((dices[i][remaining[a][0]] == dices[j][remaining[d][0]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][1]]) ||
                               (dices[i][remaining[a][0]] == dices[j][remaining[d][1]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][0]])){
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

int main(){
    while(std::cin >> N, N){
        UnionFind uf(N);

        REP(i, N){
            REP(j, 6){
                std::string s;
                std::cin >> s;

                REP(k, 6){
                    if(s == colors[k]){dices[i][j] = k;}
                }
            }
        }

        REP(i, N){
            FOR(j, i+1, N){
                if(same(i, j)){
                    uf.unite(i, j);
                }
            }
        }

        int res = N;
        REP(i, N){
            if(i == uf.find(i)){res -= 1;}
        }

        std::cout << res << std::endl;
    }
}

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

const int trans[6][6] =
{
	{1, 3, 5, 2, 4, 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)
	{
		UnionFind uft(n);
		vector<Cube> cubes(n);
		
		rep(i, n) rep(j, 6) cin >> cubes[i].colors[j];
		
		rep(i, n) REP(j, i+1, n) if(same(cubes[i], cubes[j])) uft.unite(i, j);
		
		cout << n - uft.count() << endl;
	}
}