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 <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef pair<P,P> Edge;
typedef long long ll;

// 25(28)ñÅ³ÉàÇé
class Dice{
private:
  int yoko;
  int tate;
  int round;

private:
    string face[3][4];

public:
  // int getFace(int i,int j)const{
  //   return face[i][j];
  // }
  string getFace(int idx)const{
    if(idx==0)
      return face[1][1];
    else if(idx==1)
      return face[2][1];
    else if(idx==2)
      return face[1][2];
    else if(idx==3)
      return face[1][0];
    else if(idx==4)
      return face[0][1];
    else if(idx==5)
      return face[1][3];
  }
  Dice(vector<string> &v){
    init();
    face[1][1]=v[0];
    face[2][1]=v[1];
    face[1][2]=v[2];
    face[1][0]=v[3];
    face[0][1]=v[4];
    face[1][3]=v[5];
  }
private:
  // _CXÌú»
  void init(){
//    for(int i = 0; i < 3; i++){
//      for(int j = 0; j < 4; j++){
//    	  face[i][j]=-1;
//      }
//    }
    yoko=0;
    tate=0;
    round=0;
  }
public:
  // _CXÌñ]
  void rotate(){
    if(round<4){
      string tmp = face[2][1];
      face[2][1]=face[1][2];
      face[1][2]=face[0][1];
      face[0][1]=face[1][0];
      face[1][0]=tmp;
      round++;
    }
    if(round>=4){
    	round=0;
    	// àÇÁÄ«½çA¡ÌXbgðñ]³¹é
    	if(yoko<4){
    		string tmp = face[1][3];
    		for(int i = 2; i >= 0; i--){
    			face[1][i+1]=face[1][i];
    		}
    		face[1][0]=tmp;
    		yoko++;
    	}
    	// àÇÁÄ«½çAcÌXbgðñ]³¹é
    	if(yoko>=4){
    		if(tate<4){
    			tate++;
    			string tmp = face[1][3];
    			face[1][3]=face[0][1];
    			for(int i = 0; i < 2; i++){
    				face[i][1]=face[i+1][1];
    			}
    			face[2][1]=tmp;
    		}
    		if(tate>=4){
    			round=0;
    			tate=0;
    			yoko=0;
    		}
    	}
    }
  };
};

int main(){

	int n;
	while(cin>>n&&n!=0){
		vector<Dice> vd;
		for(int i=0;i<n;i++){
			vector<string> vs;
			for(int j=0;j<6;j++){
				string s;
				cin>>s;
				vs.push_back(s);
			}
			vd.push_back(Dice(vs));
		}
		int cnt=0;
		set<vector<string> > svs;
		for(int i=0;i<vd.size();i++){
			Dice &dice=vd[i];
			vector<string> vs2;
			for(int k=0;k<6;k++)
				vs2.push_back(dice.getFace(k));
			if(svs.find(vs2)!=svs.end())
				continue;
			cnt++;
			for(int j=0;j<24;j++){
				vector<string> vs;
				for(int k=0;k<6;k++)
					vs.push_back(dice.getFace(k));
				svs.insert(vs);
				dice.rotate();
			}
		}
		cout<<n-cnt<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int colornum(string color){
  //cout<<color<<endl;
  if(color=="Red")return 0;
  if(color=="Yellow")return 1;
  if(color=="Blue")return 2;
  if(color=="Magenta")return 3;
  if(color=="Green")return 4;
  if(color=="Cyan")return 5;
  return 0;
}

int main(){
  int n;
  while(true){
    cin>>n;
    if(!n)break;
    set<int> dice;
    for(int i=0;i<n;i++){
      int d=0;
      int red = 0;
      vector<string> colors(6);
      for(int j=0;j<6;j++){
	cin>>colors[j];
	if (colors[j]=="Red")
	  red = j;
      }
      d = colornum(colors[5-red])*1296;
      int side[4];
      if(red==0)side[0] = 1,side[1]=2;
      if(red==1)side[0] = 0,side[1]=3;
      if(red==2)side[0] = 0,side[1]=1;
      if(red==3)side[0] = 0,side[1]=4;
      if(red==4)side[0] = 0,side[1]=2;
      if(red==5)side[0] = 1,side[1]=4;
      side[2] = 5-side[0];
      side[3] = 5-side[1];
      int dmin = 1296;
      for(int j=0;j<4;j++){
	int dd = 0;
	for(int k=0;k<4;k++){
	  dd *= 6;
	  dd += colornum(colors[side[(j+k)%4]]);
	}
	if(dd<dmin)dmin=dd;
      }
      d += dmin;
      dice.insert(d);
    }
    cout<<(n-dice.size())<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
using namespace std;
struct cube
{
    vector< string > v;
    int E = 2,S=1,U=0;
    void rotate( char C )
    {
        int a=E, b=S;
        switch(C)
        {
            case 'E':E=U;U=5-a;break;
            case 'S':S=U;U=5-b;break;
            case 'L':E=S;S=5-a;break;
        }
    }
    bool operator==(cube b)
    {
        for( int i = 0; i < 2; i++ )
        {
            for(int i=0;i<4;i++)
            {
                for(int i = 0; i < 4; i++ )
                {
                    if (v[E] == b.v[b.E] && v[5 - E] == b.v[5 - b.E] && v[S] == b.v[b.S] && v[5 - S] == b.v[5 - b.S] && v[U] == b.v[b.U] && v[5 - U] == b.v[5 - b.U])return true;
                    rotate('S');
                }
                rotate('E');
            }
            rotate('L');
        }
        return false;
    }
};
cube s[30];
bool used[30];
main()
{
    int n;
    while( cin >> n, n )
    {
        for( int i = 0; i < n; i++ )
        {
            s[i].v = vector< string > (6);
            for( int j = 0; j < 6; j++ )cin >> s[i].v[j];
            
        }
            vector< int > v[30];
            for( int i = 0; i < n; i++ )
            {
                for( int j = i + 1; j < n; j++ )
                {
                    if( s[i] == s[j] )v[i].emplace_back( j );
                }
            }
            int cnt = 0;
            memset( used, 0, sizeof( used ) );
            for( int i = 0; i < n; i++ )
            {
                if( used[i] )continue;
                cnt++;
                for( int  j : v[i] )
                {
                    used[j] = true;
                }
            }
            printf( "%d\n", n - cnt );
    }
}