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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

struct Dice{
	string me[6];
	
	void Init(string a[6]){
		for (int i = 0; i < 6; i++){
			me[i] = a[i];
		}
	}

	bool check(Dice d2){
		for (int i = 0; i < 6; i++){
			if (d2.me[i] != me[i])return false;
		}
		return true;
	}

	void print(){
		for (int i = 0; i < 6; i++){
			cout << me[i] << " ";
		}
		cout << endl;
	}
};

int n;
string a[6];
Dice d[110];
bool F[100];

int main(){
	while (cin >> n&&n){
		fill(F, F + 100, 1);
		REP(i, n){
			REP(j, 6)cin >> a[j];
			d[i].Init(a);
		}
		bool f = 0;
		REP(k, n){
			if (F[k]){
				FOR(l, k + 1, n){
					REP(i, 4){
						REP(j, 4){
							if (d[k].check(d[l]))F[l] = 0;
							swap(d[k].me[1], d[k].me[2]);
							swap(d[k].me[2], d[k].me[4]);
							swap(d[k].me[4], d[k].me[3]);
						}
						REP(j, 4){
							if (d[k].check(d[l]))F[l] = 0;
							swap(d[k].me[0], d[k].me[1]);
							swap(d[k].me[1], d[k].me[5]);
							swap(d[k].me[5], d[k].me[4]);
						}
						swap(d[k].me[0], d[k].me[2]);
						swap(d[k].me[2], d[k].me[5]);
						swap(d[k].me[5], d[k].me[3]);
					}
				}
			}
		}
		int co = 0;
		REP(i, n)if (F[i])co++;
		cout << n - co << endl;
	}
}

C++: #include <iostream>
#include <set>
#include <vector>
#include <string>
using namespace std;
class Cube{
public:
	int c[6];
	Cube(){}
	bool operator == (const Cube &p) const{
		for(int i=0;i<6;i++)if(c[i]!=p.c[i])return false;
		return true;
	}
	void r1(){
		int tmp[6];
		for(int i=0;i<6;i++)tmp[i]=c[i];
		c[0]=tmp[4];
		c[1]=tmp[0];
		c[4]=tmp[5];
		c[5]=tmp[1];
		return;
	}
	void r2(){
		int tmp[6];
		for(int i=0;i<6;i++)tmp[i]=c[i];
		c[0]=tmp[2];
		c[2]=tmp[5];
		c[3]=tmp[0];
		c[5]=tmp[3];
		return;
	}
	void r3(){
		int tmp[6];
		for(int i=0;i<6;i++)tmp[i]=c[i];
		c[1]=tmp[3];
		c[2]=tmp[1];
		c[3]=tmp[4];
		c[4]=tmp[2];
		return;
	}
};
bool onaji(Cube &a,Cube &b){
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			if(a==b)return true;
			a.r1();
		}
		a.r2();
	}
	a.r3();
	for(int j=0;j<4;j++){
		if(a==b)return true;
		a.r1();
	}
	a.r3();a.r3();
	for(int j=0;j<4;j++){
		if(a==b)return true;
		a.r1();
	}
	return false;
}
int GetID(string s){
	if(s=="Red")return 1;
	if(s=="Yellow")return 2;
	if(s=="Blue")return 3;
	if(s=="Magenta")return 4;
	if(s=="Green")return 5;
	if(s=="Cyan")return 6;
}
int main()
{
	int n;
	while(cin>>n&&n){
		Cube sta[30];
		vector<Cube> ans;
		for(int i=0;i<n;i++){
			string s;
			for(int j=0;j<6;j++){
				cin>>s;
				sta[i].c[j]=GetID(s);
			}
			bool neww=true;
			for(vector<Cube>::iterator it=ans.begin();it<ans.end();it++){
				Cube tmp;tmp=ans.at(it-ans.begin());
				if(onaji(tmp,sta[i]))neww=false;
			}
			if(neww)ans.push_back(sta[i]);
		}
		cout<<n-(int)ans.size()<<endl;
	}
    
    return 0;
}

C++: #include<iostream>
#include<string>
#define rep(i, n) for ( int i = 0; i < n; i++ )
using namespace std;
#define MAX 100
class Cube{
    public:
    string f[6];
    Cube(){}
    void roll_z(){ roll(1, 2, 4, 3);}
    void roll_y(){ roll(0, 2, 5, 3);}
    void roll_x(){ roll(0, 1, 5, 4);}
    void roll(int i, int j, int k, int l){
	string t = f[i]; f[i] = f[j]; f[j] = f[k]; f[k] = f[l]; f[l] = t;
    }
};

Cube C[MAX];
int n;

bool eq( Cube c1, Cube c2 ){
    rep(i, 6) if ( c1.f[i] != c2.f[i] ) return false;
    return true;
}

bool equal( Cube c1, Cube c2 ){
    rep(i, 6){
	rep(j, 4){
	    if ( eq(c1, c2) ) return true;
	    c1.roll_z();
	}
	if ( i % 2 == 0 ) c1.roll_y();
	else c1.roll_x();
    }
    return false;
}

main(){
    while( cin >> n && n ){
	rep(i, n) rep(j, 6) cin >> C[i].f[j];
	int cnt = 0;
	rep(i,n) {
	    bool hasSame = false;
	    for(int j=i+1; j<=n-1;j++) if (equal(C[i],C[j])) hasSame = true;
	    if ( !hasSame ) cnt++;
	}
	cout << n - cnt << endl;
    }
}