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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define PB(a) push_back(a)
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<string, string> P;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N;

int c(P a, P b){
  if(a.F == b.F && a.S == b.S) return 1;
  if(a.F == b.S && a.S == b.F) return 1;
  return 0;
}

int judge(vector<string> A, vector<string> B){
  P a1, a2, a3, b1, b2, b3;
  a1 = P(A[0], A[5]); a2 = P(A[1], A[4]); a3 = P(A[2], A[3]);
  b1 = P(B[0], B[5]); b2 = P(B[1], B[4]); b3 = P(B[2], B[3]);
  if(c(a1, b1) && c(a2, b2) && c(a3, b3)) return 1;
  if(c(a1, b1) && c(a2, b3) && c(a3, b2)) return 1;
  if(c(a1, b2) && c(a2, b1) && c(a3, b3)) return 1;
  if(c(a1, b2) && c(a2, b3) && c(a3, b1)) return 1;
  if(c(a1, b3) && c(a2, b1) && c(a3, b2)) return 1;
  if(c(a1, b3) && c(a2, b2) && c(a3, b1)) return 1;
  return 0;
}

int main(){
  while(scanf("%d", &N) && N){
    vector<vector<string> >D(N);
    rep(i, N){
      rep(j, 6){
	string s;
	cin >> s;
	D[i].PB(s);
      }
    }

    rep(i, D.size()){
      rep(j, D.size()){
	if(i == j) continue;
	if(judge(D[i], D[j])){
	  D.erase(D.begin()+j);
	  j--;
	}
      }
    }
    //    printf("%d\n", (int)D.size());
    printf("%d\n", N - (int)D.size());
  }
  return 0;
}

C++: #include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

#define rotate_swap(x,a,b,c,d) swap(x.a,x.b);swap(x.b,x.c);swap(x.c,x.d);
 
class Dice{
public:
  int top,front,right,left,back,bottom;
  bool operator<(const Dice &a)const{
    if (top != a.top)return top < a.top;
    if (front != a.front)return front < a.front;
    if (right != a.right)return right < a.right;
    if (left != a.left)return left < a.left;
    if (back != a.back)return back < a.back;
    if (bottom != a.bottom)return bottom < a.bottom;
    return false;
  }
};
 
void rotate_r(Dice &x){
 rotate_swap(x,top,left,bottom,right);
}
 
void rotate_l(Dice &x){
 rotate_swap(x,top,right,bottom,left);
}
 
void rotate_f(Dice &x){
 rotate_swap(x,top,back,bottom,front);
}
 
void rotate_b(Dice &x){
 rotate_swap(x,top,front,bottom,back);
}
 
void rotate_cw(Dice &x){
  rotate_swap(x,back,left,front,right);
}
 
void rotate_ccw(Dice &x){
  rotate_swap(x,back,right,front,left);
}
 
Dice data[24];

void generate_all(Dice &x){
  rep(i,6){
   rep(j,4){
     data[i*4+j]=x;
     rotate_cw(x);
   }
   if (i%2 == 0)rotate_r(x);
   else rotate_f(x);
 }
}

main(){
  string hoge[6]={"Red","Yellow","Blue","Magenta","Green","Cyan"};
  int n;
  while(cin>>n && n){
    set<Dice> S;
    rep(i,n){
      int a[6];
      rep(j,6){
	string tmp;
	cin>>tmp;
	rep(k,6)if (hoge[k] == tmp)a[j]=k;
      }
      Dice d={a[0],a[1],a[2],a[3],a[4],a[5]};
      generate_all(d);
      bool isok=true;
      rep(j,24){
	if (S.find(data[j]) != S.end()){
	  isok=false;
	}
      }
      if (isok)S.insert(d);
    }
    cout << n-S.size() << endl;
  }
  return false;
}