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;

// If this structure is used for two-dimensional surface,
// it assumes that the face id[0] faces the positive direction of the Y axis,
// the face id[1] faces the positive direction of the X axis
// and id[2] is top of the dice.
struct Dice {
  int id[6], cnt;
  const static int f[3][3];

  Dice() { init(); }

  void init() {
    for(int i = 0; i < 6; ++i) {
      id[i] = i;
    }
    cnt = 0;
  }

  // type (0:spin, 1:rollX, 2:rollY)
  // dir 1( ccw     neg      pos   )
  //    -1( cw      pos      neg   )
  void rotate(int type, int dir = 1) {
    const int *v = f[type];
    int a,b,c,t; a=v[0]; b=v[1]; c=v[2]; t=id[type];
    if(dir < 0) swap(a,c);
    id[type]=id[a]; id[a]=id[b]; id[b]=id[c]; id[c]=t;
  }

  bool next() {
    ++cnt;
    if(cnt == 24) {
      rotate(0,-1);
      rotate(1,-1);
      cnt = 0;
      return false;
    }
    if(cnt == 4*3) {
      rotate(2,-1);
      rotate(0,-1);
    } else if(cnt%4 == 0) {
      rotate(2);
    } else {
      rotate(0);
    }
    return true;
  }

  void roll(int dir) {
    //const static int dx[] = {1,0,-1,0};
    //const static int dy[] = {0,1,0,-1};
    switch(dir) {
      case 0: rotate(1,-1); break;
      case 1: rotate(2, 1); break;
      case 2: rotate(1, 1); break;
      case 3: rotate(2,-1); break;
    }
  }

  bool operator < (const Dice &d) const {
    for(int i = 0; i < 3; ++i)
      if(id[i] != d.id[i]) return id[i] < d.id[i];
    return false;
  }

  void show() {
    cout << id[4] << " / \\ " << id[5] << endl;
    cout << " /   \\" << endl;
    cout << "|\\ " << id[2] << " /|" << endl;
    cout << "|" << id[0] << "\\ /" << id[1] << "|" << endl;
    cout << " \\ | /" << endl;
    cout << "  \\|/" << endl;
    cout << "   " << id[3] << endl;
  }
};
const int Dice::f[3][3] = {{4,5,1},{3,4,2},{5,3,0}};


int main() {
  map<string,int> id;
  for(int n; cin >> n && n;) {
    set<vector<int> > st;
    for(int i = 0; i < n; ++i) {
      vector<int> v;
      for(int j = 0; j < 6; ++j) {
        string s;
        cin >> s;
        if(!id.count(s)) id[s] = id.size()-1;
        v.push_back(id[s]);
      }
      Dice d;

      do {
        vector<int> u;
        for(int j = 0; j < 6; ++j) {
          u.push_back(v[d.id[j]]);
        }
        st.insert(u);
      } while(d.next());
    }
    cout << n - (int)st.size()/24 << endl;
  }
  return 0;
}

C++: // 2011/07/21 Tazoe

#include <iostream>
#include <string>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int c[30][6];
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++){
				string str;
				cin >> str;
				if(str=="Red")
					c[i][j] = 1;
				else if(str=="Yellow")
					c[i][j] = 2;
				else if(str=="Blue")
					c[i][j] = 3;
				else if(str=="Magenta")
					c[i][j] = 4;
				else if(str=="Green")
					c[i][j] = 5;
				else if(str=="Cyan")
					c[i][j] = 6;
			}
		}
/*
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++)
				cout << c[i][j] << ' ';
			cout << endl;
		}
*/
		for(int i=0; i<n; i++){
			if(c[i][0]>c[i][5]){
				int tmp = c[i][0];
				c[i][0] = c[i][5];
				c[i][5] = tmp;

				tmp = c[i][2];
				c[i][2] = c[i][3];
				c[i][3] = tmp;
			}
			if(c[i][1]>c[i][4]){
				if(c[i][2]<c[i][3]){
					int tmp = c[i][1];
					c[i][1] = c[i][2];
					c[i][2] = c[i][4];
					c[i][4] = c[i][3];
					c[i][3] = tmp;
				}
				else{
					int tmp = c[i][1];
					c[i][1] = c[i][4];
					c[i][4] = tmp;

					tmp = c[i][2];
					c[i][2] = c[i][3];
					c[i][3] = tmp;
				}
			}
			if(c[i][2]>c[i][3]){
				int tmp = c[i][2];
				c[i][2] = c[i][1];
				c[i][1] = c[i][3];
				c[i][3] = c[i][4];
				c[i][4] = tmp;
			}
			if(c[i][1]==1){
				int tmp = c[i][1];
				c[i][1] = c[i][2];
				c[i][2] = c[i][0];
				c[i][0] = tmp;
				tmp = c[i][4];
				c[i][4] = c[i][3];
				c[i][3] = c[i][5];
				c[i][5] = tmp;
			}
			if(c[i][2]==1){
				int tmp = c[i][2];
				c[i][2] = c[i][1];
				c[i][1] = c[i][0];
				c[i][0] = tmp;
				tmp = c[i][3];
				c[i][3] = c[i][4];
				c[i][4] = c[i][5];
				c[i][5] = tmp;
			}
		}
/*
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++)
				cout << c[i][j] << ' ';
			cout << endl;
		}
*/
		int d[30];
		for(int i=0; i<n; i++)
			d[i] = ((((c[i][0]*10+c[i][1])*10+c[i][2])*10+c[i][3])*10+c[i][4])*10+c[i][5];

		for(int i=0; i<n-1; i++)
			for(int j=i+1; j<n; j++)
				if(d[i]>d[j]){
					int tmp = d[i];
					d[i] = d[j];
					d[j] = tmp;
				}
/*
		for(int i=0; i<n; i++)
			cout << d[i] << endl;
*/
		int m = n;
		for(int i=0; i<n-1; i++)
			if(d[i]==d[i+1])
				m--;

		cout << n-m << endl;
	}

	return 0;
}