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++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<cmath>
#include<sstream>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<utility>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<set>
#include<stdlib.h>
#include<time.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define repp(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { 1, 0, -1, 0 };
const int dX[8] = { 0, 1, 1, 1, 0, -1, -1, -1 }, dY[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
struct XY
{
	int x;
	int y;
};
vector<long long> Prime;
void SieveOfEratosthenes(long long e) //setup vector<long long>Prime(~x)
{
	bool *x=new bool[e];
	for (long long i = 0; i <= e; i++)x[i] = false;
	for (long long i = 2; i <= e; i++)
	{
		if (x[i]);
		else
		{
			Prime.push_back(i);
			for (long long l = i; l <= e; l += i)x[l] = true;
		}
	}
	return;
}
int StringToInt(string a)
{
	stringstream ss;
	int x;
	ss << a;
	ss >> x;
	return x;
}
string IntToString(int a)
{
	stringstream ss;
	string x;
	ss << a;
	ss >> x;
	return x;
}
int GetRandom(int MIN, int MAX)
{
	static int flag=0;
	if (flag == 0)
	{
		srand((unsigned int)time(NULL));
		flag = 1;
	}
	return MIN + (int)(rand()*(MAX - MIN + 1.0) / (1.0 + RAND_MAX));
}
int in()
{
	int x;
	cin >> x;
	return x;
}
void outl(string a)
{
	cout << a << endl;
	return;
}
void outl(int a)
{
	cout << a << endl;
	return;
}
void out(string a)
{
	cout << a;
	return;
}
void out(int a)
{
	cout << a;
	return;
}
/*
(char)'1' - (int)48 = (int)1;
???1111181
 (char)A=(int)65
 (char)a=(int)97
*/
////////////////////////////////////////////////////

int main()
{
	int n;
	while (cin >> n&&n)
	{
		int ans = 0;
		map<string, int> x;
		for (int i = 0; i < n; i++)
		{
			string z;
			int as = 1;
			for (int l = 0; l < 6; l++)
			{
				char now[10];
				cin >> now;
				z += now[0];
			}
			for (int q = 0; q < 6; q++)
			{
				for (int i = 0; i < 4; i++)
				{
					if (x.count(z))as = 0;
					swap(z[4], z[3]);
					swap(z[4], z[1]);
					swap(z[4], z[2]);
				}
				string nn;
				nn += z[5];
				rep(i, 5)nn += z[i];
				z = nn;
			}
			ans += as;
			for (int q = 0; q < 6; q++)
			{
				for (int i = 0; i < 4; i++)
				{
					x[z] = 1;
					swap(z[4], z[3]);
					swap(z[4], z[1]);
					swap(z[4], z[2]);
				}
				string nn;
				nn += z[5];
				rep(i, 5)nn += z[i];
				z = nn;
			}
		}
		cout << n - ans << endl;
	}
}

C++: #include<iostream>
using namespace std;

#define rep(i,n)  for(int i=0; i<n; i++)

string rot1( string s ){
	string res = s;
	res[0] = s[1];
	res[1] = s[5];
	res[5] = s[4];
	res[4] = s[0];
	return res;
}

string rot2( string s ){
	string res = s;
	res[0] = s[2];
	res[2] = s[5];
	res[5] = s[3];
	res[3] = s[0];
	return res;
}

string rot3( string s ){
	string res = s;
	res[1] = s[2];
	res[2] = s[4];
	res[4] = s[3];
	res[3] = s[1];
	return res;
}

int main(){
	int n;
	while( cin >> n, n ){
		string obj[30];
		rep(i,n){
			rep(j,6){
				string in;
				cin >> in;
				obj[i] += in[0];
			}
		}
		int cnt = 0;
		rep(i,n){
			for(int j=i+1; j<n; j++){
				bool found = false;
				rep(k,6){
					rep(l,4){
						if( obj[i] == obj[j] ){
							found = true;
							break;
						}
						obj[j] = rot3(obj[j]);
					}
					if(found)	break;
					if(k%2)	obj[j] = rot1(obj[j]);
					else	obj[j] = rot2(obj[j]);
				}
				if(found)	cnt++;
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

struct color {
	string c1, c2, c3, c4, c5, c6;
};

int main()
{
	int n; 
	while (cin >> n, n) {
		vector<color> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i].c1 >> v[i].c2 >> v[i].c3 >> v[i].c4 >> v[i].c5 >> v[i].c6;
		}

		int cnt = 0;
		for (int i = 0; i < n - 1; i++) {
			map<string, string> m;
			m[v[i].c1] = v[i].c6;
			m[v[i].c6] = v[i].c1;
			m[v[i].c2] = v[i].c5;
			m[v[i].c5] = v[i].c2;
			m[v[i].c3] = v[i].c4;
			m[v[i].c4] = v[i].c3;
			for (int j = i + 1; j < n; j++) {
				if (m[v[j].c1] == v[j].c6) {
					if (m[v[j].c2] == v[j].c5) {
						if (m[v[j].c3] == v[j].c4)
							cnt++;
					}
				}
			}
		}
		cout << cnt << endl;
	}
	return 0;
}

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

int pad[3][6] = { { 3, 1, 0, 5, 4, 2 }, { 1, 5, 2, 3, 0, 4 }, { 0, 2, 4, 1, 3, 5 } };
void rotate ( int op, string &S )
{
    string temp;
    for ( int i = 0; i < 6; i++ ) {
        temp.push_back ( S[pad[op][i]] );
    }
    S = temp;
}

bool search ( string S, string T )
{
    for ( int i = 0; i < 4; i++ ) {
        for ( int j = 0; j < 4; j++ ) {
            for ( int k = 0; k < 4; k++ ) {
                if ( S == T ) {
                    return ( false );
                }
                rotate ( 2, S );
            }
            rotate ( 1, S );
        }
        rotate ( 0, S );
    }
    return ( true );
}

int main()
{
    int N;
    int rotate_pos[35];

    while ( cin >> N, N ) {
        vector < string > data(N);
        int ans = 0;
        bool flag[35];

        fill_n(flag, 35, true);
        for ( int i = 0; i < N; i++ ) {
            string S;
            for ( int j = 0; j < 6; j++ ) {
                cin >> S;
                data[i] += S[0];
            }
            for ( int j = 0; j < i; j++ ) {
                flag[i] &= search ( data[i], data[j] );
            }
        }
        for ( int i = 0; i < N; i++ ) {
            if ( !flag[i] ) {
                ans++;
            }
        }
        cout << ans << endl;
    }

    return ( 0 );
}