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 <algorithm>
#include <vector>
#include <map>
#include <set>

using namespace std;
 
enum { TOP=0, FRONT=1, LEFT=2, RIGHT=3, BACK=4, BOTTOM=5 };
enum { WEST = 0, NORTH=1, EAST=2, SOUTH=3 };
 
class Dice
{
private:
  int num[6];
  
public:
   
  int getTop()    { return num[TOP];    }
  int getBottom() { return num[BOTTOM]; }
  int getFront()  { return num[FRONT];  }
  int getBack()   { return num[BACK];   }
  int getLeft()   { return num[LEFT];   }
  int getRight()  { return num[RIGHT];  }
   
  void roll(int dir)
  {
    int tmp;
    switch(dir) {
    case EAST:
      tmp = num[RIGHT];
      num[RIGHT] = num[TOP];
      num[TOP] = num[LEFT];
      num[LEFT] = num[BOTTOM];
      num[BOTTOM] = tmp;
      break;
       
    case WEST:
      tmp = num[LEFT];
      num[LEFT] = num[TOP];
      num[TOP] = num[RIGHT];
      num[RIGHT] = num[BOTTOM];
      num[BOTTOM] = tmp;
      break;
       
    case NORTH:
      tmp = num[BOTTOM];
      num[BOTTOM] = num[FRONT];
      num[FRONT] = num[TOP];
      num[TOP] = num[BACK];
      num[BACK] = tmp;
      break;
       
    case SOUTH:
      tmp = num[BOTTOM];
      num[BOTTOM] = num[BACK];
      num[BACK] = num[TOP];
      num[TOP] = num[FRONT];
      num[FRONT] = tmp;
      break;
       
    }
  }
  
  void rightHandedRoll() {
    int tmp = num[FRONT];
    num[FRONT] = num[RIGHT];
    num[RIGHT] = num[BACK];
    num[BACK] = num[LEFT];
    num[LEFT] = tmp;
  }
   
  Dice() {
  }
  
  Dice(int t, int f, int l, int r, int b, int m)
  {
    num[TOP] = t, num[FRONT] = f, num[LEFT] = l, num[RIGHT] = r, num[BACK] = b, num[BOTTOM] = m;
  }
  Dice(vector<int> v)
  {
    num[TOP] = v[0], num[FRONT] = v[1], num[LEFT] = v[2], num[RIGHT] = v[3], num[BACK] = v[4], num[BOTTOM] = v[5];
  }
  
  bool equals (Dice rhs) const {
    for(int i=0; i<4; i++) {
      rhs.roll(NORTH);
      if(num[TOP] == rhs.getTop()) break;
      rhs.roll(WEST);
      if(num[TOP] == rhs.getTop()) break;
    }
    for(int i=0; i<4; i++) {
      if(check(rhs)) return true;
      else rhs.rightHandedRoll();
    }
    return false;
  }
  
private:
  bool check(const Dice& rhs) const {
    for(int i=0; i<6; i++) {
      if(num[i]!=rhs.num[i]) return false;
    }
    return true;
  }
  
};

map<string, int> mp;

int main() {
  
  mp["Red"]=0, mp["Yellow"]=1, mp["Blue"]=2, mp["Magenta"]=3, mp["Green"]=4, mp["Cyan"]=5;
  int N;
  while(cin >> N && N) {
    vector<Dice> d;
    for(int i=0; i<N; i++) {
      vector<int> v(6);
      for(int j=0; j<6; j++) {
        string s; cin >> s; v[j] = mp[s];
      }
      swap(v[2], v[3]);
      d.push_back(Dice(v));
    }
    
    int cnt = 0;
    bool update = true;
    while(update) {
      update = false;
      for(int i=0; i<d.size(); i++) {
        for(int j=i+1; j<d.size(); j++) {
          if(d[i].equals(d[j])) {
            d.erase(d.begin()+j);
            update = true; cnt ++;
            goto NEXT;
          }
        }
      }
    NEXT:;
    }
    cout << cnt << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct Dice {
	vector<int> val;
	Dice(){ val.resize(6); }
	bool operator == (const Dice &t) const {
		return val == t.val;
	}
	bool operator != (const Dice &t) const {
		return val != t.val;
	}
	bool operator < (const Dice &t) const {
		return val < t.val;
	}
	void rotate_right(){
		int tmp = val[1];
		val[1] = val[2];
		val[2] = val[4];
		val[4] = val[3];
		val[3] = tmp;
	}
	void roll_front(){
		int tmp = val[0];
		val[0] = val[1];
		val[1] = val[5];
		val[5] = val[4];
		val[4] = tmp;
	}
	void roll_right(){
		int tmp = val[0];
		val[0] = val[3];
		val[3] = val[5];
		val[5] = val[2];
		val[2] = tmp;
	}
	vector<Dice> all_pattern(){
		vector<Dice> res;
		Dice d = *this;
		for (int i = 0; i < 6; i++){
			for (int j = 0; j < 4; j++){
				res.push_back(d);
				d.rotate_right();
			}
			if (i == 0) d.roll_front();
			else if (i < 5) d.roll_right();
			else d.roll_front();
		}
		return res;
	}
};

struct UnionFind {
	vector<int> parent;
	
	UnionFind(int n){
		parent.resize(n);
		for (int i = 0; i < n; i++){
			parent[i] = i;
		}
	}
	
	int find(int x){
		if (parent[x] == x) return x;
		return parent[x] = find(parent[x]);
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	bool unite(int x, int y){
		x = find(x);
		y = find(y);
		if (x == y) return false;
		parent[x] = y;
		return true;
	}
};

map<string, int> ctoi;
int n;
vector<Dice> dices;

bool check(const vector<Dice> &A, const vector<Dice> &B)
{
	for (int i = 0; i < A.size(); i++){
		for (int j = 0; j < B.size(); j++){
			if (A[i] == B[j]) return true;
		}
	}
	return false;
}

void solve()
{
	vector<vector<Dice> > pats;
	UnionFind uf(n);
	
	for (int i = 0; i < n; i++){
		pats.push_back(dices[i].all_pattern());
	}
	
	int res = 0;
	
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			vector<Dice> &A = pats[i];
			vector<Dice> &B = pats[j];
			if (check(A, B)){
				res += uf.unite(i, j);
				continue;
			}
		}
	}
	
	printf("%d\n", res);
}

int main()
{
	ctoi["Red"] = 0;
	ctoi["Yellow"] = 1;
	ctoi["Blue"] = 2;
	ctoi["Magenta"] = 3;
	ctoi["Green"] = 4;
	ctoi["Cyan"] = 5;
	
	while (scanf("%d", &n), n){
		dices.clear();
		for (int i = 0; i < n; i++){
			Dice d;
			for (int j = 0; j < 6; j++){
				string str;
				cin >> str;
				d.val[j] = ctoi[str];
			}
			dices.push_back(d);
		}
		solve();
	}
	
	return 0;
}