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>

using namespace std;

enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
template <class T>
class dice {
public:
  dice() {
    id[TOP] = 0; id[FRONT] = 1; id[LEFT] = 2;
    id[RIGHT] = 3; id[BACK] = 4; id[BOTTOM] = 5;
  }
  dice(int t, int f, int l, int r, int b, int m) {
    id[TOP] = t; id[FRONT] = f; id[LEFT] = l;
    id[RIGHT] = r; id[BACK] = b; id[BOTTOM] = m;
  }
  T& operator[] (FACE f) { return var[id[f]]; }
  const T& operator[] (FACE f) const { return var[id[f]]; }
  bool operator==(const dice<T>& b) const {
    const dice<T> &a = *this;
    return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&
           a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&
           a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
  }
  void roll_x() { roll(TOP, BACK, BOTTOM, FRONT); }
  void roll_y() { roll(TOP, LEFT, BOTTOM, RIGHT); }
  void roll_z() { roll(FRONT, RIGHT, BACK, LEFT); }
  vector<dice> all_rolls() {
    vector<dice> ret;
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        ret.push_back(*this);
    return ret;
  }
  bool equivalent_to(const dice& di) {
    for (int k = 0; k < 6; (k&1?roll_y():roll_x()),++k)
      for (int i = 0; i < 4; roll_z(), ++i)
        if (*this == di) return true;
    return false;
  }
private:
  void roll(FACE a, FACE b, FACE c, FACE d) {
    T tmp = id[a];
    id[a] = id[b]; id[b] = id[c];
    id[c] = id[d]; id[d] = tmp;
  }
  T var[6];
  int id[6];
};

string cols[]={"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan"};
map<string, int> mp;

int main() {
  
  for(int i=0; i<6; i++) mp[cols[i]] = i;
  int N;
  while(cin >> N && N) {
    vector<dice<int> > 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];
      }
      d.push_back(dice<int>(v[0],v[1],v[2],v[3],v[4],v[5]));
    }
    
    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].equivalent_to(d[j])) {
            cnt ++;
            d.erase(d.begin()+i); update = true; goto NEXT;
          }
        }
      }
    NEXT:;
    }
    cout << N-cnt << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    while(cin >> n, n){
        vector<vector<string>> v;
        vector<string> c(6);
        for(int j=0; j < n; j++){
            for(int i=0; i< 6; i++){
                cin >> c[i];
            }
            string a, b, d;
            a = c[0].substr(0,1) + c[5].substr(0,1); b = c[1].substr(0,1) + c[4].substr(0,1); d = c[2].substr(0,1) + c[3].substr(0,1);
            vector<string> p(3);
            sort(a.begin(), a.end());
            sort(b.begin(), b.end());
            sort(d.begin(), d.end());
            p[0] = a; p[1] = b; p[2] = d;
            sort(p.begin(), p.end());
            bool is = false;
            for(int i=0; i < v.size(); i++){
                bool b = true;
                for(int j=0; j < 3; j++) if(v[i][j] != p[j])b = false;
                if(b){is = true; break;}
            }
            if(!is)v.push_back(p);
        }
        cout << n-v.size() << endl;
    }
}

C++: #include<stdio.h>
int main(void)
{
	int n,flg[31],i,j,k,cnt;
	char cube[31][10][10];
	while(1){
		scanf("%d",&n);
		if(n==0) break;
		for(i=0;i<n;i++){
			for(j=0;j<6;j++) scanf("%s",&cube[i][j]);
			flg[i]=0;
		}
		for(i=0;i<n;i++){	
			for(j=0;j<3;j++){
				for(k=i+1;k<n;k++){
					if(j==0){
						if(cube[i][j][0]!=cube[k][0][0]&&cube[i][j][0]!=cube[k][5][0]) break;
					}
					else if(j==1){
						if(cube[i][j][0]!=cube[k][1][0]&&cube[i][j][0]!=cube[k][4][0]) break;
					}
					else{
						if(cube[i][j][0]!=cube[k][2][0]&&cube[i][j][0]!=cube[k][3][0]) break;
						else flg[k]=1;
					}
				}
			}
		}
		cnt=0;
		for(i=0;i<n;i++){
			if(flg[i]==1) cnt++;
		}
		printf("%d\n",cnt);
	}
	return 0;
}