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<bits/stdc++.h>
using namespace std;
int N;
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0)return 0;
		int X[30][6];
		for(int i=0;i<N;i++){
			for(int j=0;j<6;j++){
				string SS;
				cin >> SS;
				if(SS=="Cyan")X[i][j]=0;
				if(SS=="Yellow")X[i][j]=1;
				if(SS=="Red")X[i][j]=2;
				if(SS=="Magenta")X[i][j]=3;
				if(SS=="Green")X[i][j]=4;
				if(SS=="Blue")X[i][j]=5;
			}
		}
		int cou=0;
		for(int i=0;i<N;i++){
			bool F=true;
			for(int j=i+1;j<N;j++){
				//equal->false
				for(int k=0;k<6;k++){
					for(int l=0;l<4;l++){
						bool FF=false;
						for(int m=0;m<6;m++){
							//if(i==0&&j==1)printf("%d ",X[i][m]);
							if(X[i][m]!=X[j][m])FF=true;
						}
						//if(i==0&&j==1)printf("\n");
						if(!FF)F=false;
						int t=X[i][1];
						X[i][1]=X[i][2];
						X[i][2]=X[i][4];
						X[i][4]=X[i][3];
						X[i][3]=t;
					}
					if(k==0){
						int t=X[i][0];
						X[i][0]=X[i][1];
						X[i][1]=X[i][5];
						X[i][5]=X[i][4];
						X[i][4]=t;
					}
					if(k==1){
						int t=X[i][0];
						X[i][0]=X[i][2];
						X[i][2]=X[i][5];
						X[i][5]=X[i][3];
						X[i][3]=t;
					}
					if(k==2){
						int t=X[i][2];
						X[i][2]=X[i][3];
						X[i][3]=t;
						t=X[i][5];
						X[i][5]=X[i][0];
						X[i][0]=t;
					}
					if(k==3){
						int t=X[i][0];
						X[i][0]=X[i][3];
						X[i][3]=X[i][5];
						X[i][5]=X[i][2];
						X[i][2]=t;
					}
					if(k==4){
						int t=X[i][0];
						X[i][0]=X[i][1];
						X[i][1]=X[i][5];
						X[i][5]=X[i][4];
						X[i][4]=t;
					}
					
				}
			}
			if(F)cou++;
		}
		printf("%d\n",N-cou);
	}
}

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++) if(v[i] == p) is = true;
            if(!is)v.push_back(p);
        }
        cout << n-v.size() << endl;
    }
}

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

typedef vector<string> Dice;

void rotate_front(Dice& dice)
{
	string t = dice[0];
	dice[0] = dice[4];
	dice[4] = dice[5];
	dice[5] = dice[1];
	dice[1] = t;
}

void rotate_right(Dice& dice)
{
	string t = dice[0];
	dice[0] = dice[3];
	dice[3] = dice[5];
	dice[5] = dice[2];
	dice[2] = t;
}

void rotate_clock(Dice& dice)
{
	string t = dice[1];
	dice[1] = dice[2];
	dice[2] = dice[4];
	dice[4] = dice[3];
	dice[3] = t;
}

void rotate(Dice& dice, string top)
{
	for(int i=0; i<4; i++)
	{
		if(dice[0] == top) return;
		rotate_front(dice);
	}

	for(int i=0; i<4; i++)
	{
		if(dice[0] == top) return;
		rotate_right(dice);
	}
}

bool match(Dice d1, Dice d2)
{
	if(d1[0] != d2[0]) return false;
	if(d1[5] != d2[5]) return false;

	for(int i=0; i<4; i++)
	{
		bool is_match = true;
		for(int j=1; j<5; j++)
		{
			if(d1[j] != d2[j])
			{
				is_match = false;
				break;
			} 
		}
		if(is_match) return true;
		
		rotate_clock(d2);
	}

	return false;
}

int table[6];

int main()
{
	int T;
	string color;
	vector<Dice> dices;

	while(cin >> T, T)
	{
		dices = vector<Dice>(T);
		for(int t=0; t<T; t++)
		{
			for(int i=0; i<6; i++)
			{
				cin >> color;
				dices[t].push_back(color);
			}
			if(t != 0) rotate(dices[t], dices[0][0]);
		}

		int c = 0;
		for(int t=0; t<T; t++)
		{
			bool is_match = false;
			for(int i=0; i<t; i++)
			{
				if(match(dices[t], dices[i]))
				{
					is_match = true;
					break;
				}
			}

			if(!is_match) c++;
		}

		printf("%d\n", T-c);
	}
}

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

struct BOX {
  string top;
  string back, left, right, head;
  string buttom;
  bool operator==(const BOX& b) const {
    return(top    == b.top   &&
           back   == b.back  &&
           left   == b.left  &&
           right  == b.right &&
           head   == b.head  &&
           buttom == b.buttom);
  }
};
class UnionFind{
private:
  vector< int > rank, p;
  void link(int x,int y){
    if(rank[x] > rank[y]){
      p[y] = x;
    }else{
      p[x] = y;
      if(rank[x] == rank[y]) rank[y]++;
    }
  }
public:
  UnionFind(int size){
    rank.resize(size,0);
    p.resize(size,0);
    for(int i = 0; i < rank.size(); i++){
      p[i] = i, rank[i] = 0;
    }
  }
  void Unite(int x,int y){
    if(Find(x) != Find(y)) link(Find(x),Find(y));
  }
  int Find(int x){
    return( x != p[x] ? p[x] = Find(p[x]) : p[x]);
  }
};

void Roll(BOX& B)
{
  string temp = B.head;
  B.head = B.right;
  B.right = B.back;
  B.back = B.left;
  B.left = temp;
}
 
void RoLL(BOX& B) {
  string temp = B.head;
  B.head = B.top;
  B.top = B.back;
  B.back = B.buttom;
  B.buttom = temp;
}
void RolL(BOX& B) {
  string temp = B.left;
  B.left = B.top;
  B.top = B.right;
  B.right = B.buttom;
  B.buttom = temp;
}
 
 
bool Myon(BOX& a, BOX& b)
{
  for(int k = 0; k < 4; k++) {
    Roll(a);
    for(int l = 0; l < 4; l++) {
      RoLL(a);
      for(int m = 0; m < 4; m++) {
        RolL(a);
        if(a == b) return(true);
      }
    }
  }
  return(false);
}

int main()
{
  int n;
  vector< BOX > boxes;
  while(cin >> n, n) {
    UnionFind uf(n);

    boxes.resize(n);
    int ret = 0;

    for(int i = 0; i < n; i++) {
      BOX &b = boxes[i];
      cin >> b.top >> b.head >> b.right >> b.back >> b.left >> b.buttom;
    }
    for(int i = 0; i < n; i++) {
      for(int j = 0; j < n; j++) {
        if(Myon(boxes[i], boxes[j])) uf.Unite(i, j);
      }
    }
    for(int i = 0; i < n; i++) {
      ret += uf.Find(i) == i;
    }
    cout << n - ret << endl;
  }
}