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;
string s[33][6];
int c7(int a,int b){
  if(s[a][0]==s[b][0]&&s[a][1]==s[b][1]&&s[a][2]==s[b][2]&&s[a][3]==s[b][4]&&s[a][4]==s[b][3]&&s[a][5]==s[b][5])return 1;
  if(s[a][0]==s[b][0]&&s[a][1]==s[b][2]&&s[a][2]==s[b][4]&&s[a][3]==s[b][3]&&s[a][4]==s[b][1]&&s[a][5]==s[b][5])return 1;
  if(s[a][0]==s[b][0]&&s[a][1]==s[b][4]&&s[a][2]==s[b][3]&&s[a][3]==s[b][1]&&s[a][4]==s[b][2]&&s[a][5]==s[b][5])return 1;
  if(s[a][0]==s[b][0]&&s[a][1]==s[b][3]&&s[a][2]==s[b][1]&&s[a][3]==s[b][2]&&s[a][4]==s[b][4]&&s[a][5]==s[b][5])return 1;
  if(s[a][0]==s[b][1]&&s[a][1]==s[b][0]&&s[a][2]==s[b][3]&&s[a][3]==s[b][5]&&s[a][4]==s[b][2]&&s[a][5]==s[b][4])return 1;
  if(s[a][0]==s[b][1]&&s[a][1]==s[b][2]&&s[a][2]==s[b][0]&&s[a][3]==s[b][3]&&s[a][4]==s[b][5]&&s[a][5]==s[b][4])return 1;
  if(s[a][0]==s[b][1]&&s[a][1]==s[b][5]&&s[a][2]==s[b][2]&&s[a][3]==s[b][0]&&s[a][4]==s[b][3]&&s[a][5]==s[b][4])return 1;
  if(s[a][0]==s[b][1]&&s[a][1]==s[b][3]&&s[a][2]==s[b][5]&&s[a][3]==s[b][2]&&s[a][4]==s[b][0]&&s[a][5]==s[b][4])return 1;
  if(s[a][0]==s[b][2]&&s[a][1]==s[b][0]&&s[a][2]==s[b][4]&&s[a][3]==s[b][3]&&s[a][4]==s[b][5]&&s[a][5]==s[b][3])return 1;
  if(s[a][0]==s[b][2]&&s[a][1]==s[b][1]&&s[a][2]==s[b][0]&&s[a][3]==s[b][3]&&s[a][4]==s[b][4]&&s[a][5]==s[b][3])return 1;
  if(s[a][0]==s[b][2]&&s[a][1]==s[b][5]&&s[a][2]==s[b][1]&&s[a][3]==s[b][3]&&s[a][4]==s[b][0]&&s[a][5]==s[b][3])return 1;
  if(s[a][0]==s[b][2]&&s[a][1]==s[b][4]&&s[a][2]==s[b][5]&&s[a][3]==s[b][3]&&s[a][4]==s[b][1]&&s[a][0]==s[b][3])return 1;
  if(s[a][0]==s[b][5]&&s[a][1]==s[b][1]&&s[a][2]==s[b][2]&&s[a][3]==s[b][3]&&s[a][4]==s[b][4]&&s[a][5]==s[b][0])return 1;
  if(s[a][0]==s[b][5]&&s[a][1]==s[b][2]&&s[a][2]==s[b][3]&&s[a][3]==s[b][4]&&s[a][4]==s[b][1]&&s[a][5]==s[b][0])return 1;
  if(s[a][0]==s[b][5]&&s[a][1]==s[b][3]&&s[a][2]==s[b][4]&&s[a][3]==s[b][1]&&s[a][4]==s[b][2]&&s[a][5]==s[b][0])return 1;
  if(s[a][0]==s[b][5]&&s[a][1]==s[b][4]&&s[a][2]==s[b][1]&&s[a][3]==s[b][2]&&s[a][4]==s[b][3]&&s[a][5]==s[b][0])return 1;
  if(s[a][0]==s[b][4]&&s[a][1]==s[b][0]&&s[a][2]==s[b][3]&&s[a][3]==s[b][5]&&s[a][4]==s[b][2]&&s[a][5]==s[b][1])return 1;
  if(s[a][0]==s[b][4]&&s[a][1]==s[b][2]&&s[a][2]==s[b][0]&&s[a][3]==s[b][3]&&s[a][4]==s[b][5]&&s[a][5]==s[b][1])return 1;
  if(s[a][0]==s[b][4]&&s[a][1]==s[b][5]&&s[a][2]==s[b][2]&&s[a][3]==s[b][0]&&s[a][4]==s[b][3]&&s[a][5]==s[b][1])return 1;
  if(s[a][0]==s[b][4]&&s[a][1]==s[b][3]&&s[a][2]==s[b][5]&&s[a][3]==s[b][2]&&s[a][4]==s[b][0]&&s[a][5]==s[b][1])return 1;
  if(s[a][0]==s[b][3]&&s[a][1]==s[b][0]&&s[a][2]==s[b][4]&&s[a][3]==s[b][3]&&s[a][4]==s[b][5]&&s[a][5]==s[b][2])return 1;
  if(s[a][0]==s[b][3]&&s[a][1]==s[b][1]&&s[a][2]==s[b][0]&&s[a][3]==s[b][3]&&s[a][4]==s[b][4]&&s[a][5]==s[b][2])return 1;
  if(s[a][0]==s[b][3]&&s[a][1]==s[b][5]&&s[a][2]==s[b][1]&&s[a][3]==s[b][3]&&s[a][4]==s[b][0]&&s[a][5]==s[b][2])return 1;
  if(s[a][0]==s[b][3]&&s[a][1]==s[b][4]&&s[a][2]==s[b][5]&&s[a][3]==s[b][3]&&s[a][4]==s[b][1]&&s[a][5]==s[b][2])return 1;
  return 0;
}
int main(){
  int n;
  while(cin>>n,n){
    int sum=0;
    for(int i=0;i<n;i++)
      for(int j=0;j<6;j++)
        cin>>s[i][j];
    for(int i=0;i<n;i++){
      int p=0;
      for(int j=i+1;j<n;j++){
        if(c7(i,j))p++;
      }
      if(p)sum++;
    }
    cout<<sum<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;

class dice{
public:
    string top, bottom, right, left, front, back;
    void North();
    void East();
    void South();
    void West();
    void clockRotate();
};
void dice:: North(){
    string tmp = top;
    top = front;
    front = bottom;
    bottom = back;
    back = tmp;
}
void dice:: East(){
    string tmp = top;
    top = left;
    left = bottom;
    bottom = right;
    right = tmp;
}
void dice:: South(){
    string tmp = top;
    top = back;
    back = bottom;
    bottom = front;
    front = tmp;
}
void dice:: West(){
    string tmp = top;
    top = right;
    right = bottom;
    bottom = left;
    left = tmp;
}
void dice:: clockRotate(){
    string tmp = front;
    front = right;
    right = back;
    back = left;
    left = tmp;
}

bool sameDice(dice d1, dice d2){
    string s = "NRRRRNRRRRNRRRRNRRRRERRRRERRRRERRRR";
    rep(i, 0, s.size()){
        if(d1.top == d2.top && d1.front == d2.front && d1.right == d2.right && d1.back == d2.back && d1.left == d2.left && d1.bottom == d2.bottom){
            return true;
        }
        if(s[i] == 'N') d1.North();
        else if(s[i] == 'E') d1.East();
        else if(s[i] == 'S') d1.South();
        else if(s[i] == 'W') d1.West();
        else if(s[i] == 'R') d1.clockRotate();
    }
    return false;
}

signed main(){
    int n;
    while(cin >> n, n){
        vector<dice> dices;
        dice d;
        rep(i, 0, n){
            cin >> d.top >> d.front >> d.right >> d.left >> d.back >> d.bottom;
            dices. push_back(d);
        }
        vector<int> f(n);
        rep(i ,0, n) f[i] = i;
        rep(i, 0, n){
            rep(j, i + 1, n){
                if(sameDice(dices[i], dices[j])){
                    f[i] = f[j];
                }
            }
        }
        set<int> st;
        rep(i, 0, n){
            st.insert(f[i]);
        }
        cout << n - st.size() << endl;
    }
}