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<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)
enum{TOP=0,FRONT,RIGHT,LEFT,BACK,BOTTOM}; 
#define rotate_swap(x,a,b,c,d) swap(x.m[a],x.m[b]);swap(x.m[b],x.m[c]);swap(x.m[c],x.m[d]);


class Dice{
public:
  int m[6];
  bool operator<(const Dice &a)const{
    rep(i,6)if (m[i] != a.m[i])return m[i] < a.m[i];
    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;
}

C++: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

map<string,int> dict;

//saikoro
 
typedef vector<int> Dice;
enum {TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM};
#define rotate_swap(x, a, b, c, d) swap(x[a], x[b]); swap(x[b], x[c]); swap(x[c], x[d]);
 
void rotate_r(Dice& dice) {rotate_swap(dice, TOP, LEFT, BOTTOM, RIGHT);}
void rotate_l(Dice& dice) {rotate_swap(dice, TOP, RIGHT, BOTTOM, LEFT);}
void rotate_f(Dice& dice) {rotate_swap(dice, TOP, BACK, BOTTOM, FRONT);}
void rotate_b(Dice& dice) {rotate_swap(dice, TOP, FRONT, BOTTOM, BACK);}
void rotate_w(Dice& dice) {rotate_swap(dice, BACK, LEFT, FRONT, RIGHT);}
void rotate_c(Dice& dice) {rotate_swap(dice, BACK, RIGHT, FRONT, LEFT);}
 
vector<Dice> generate_all(Dice dice) {
 	vector<Dice> res;
  	for (int i = 0; i < 6; ++i) {
    	for (int j = 0; j < 4; ++j){
    		res.emplace_back(dice), rotate_w(dice);
    	}
	    if (i % 2) rotate_f(dice);
	    else rotate_r(dice);
	}
	return res;
}


void mainmain(){
	dict["Red"] = 0;
	dict["Yellow"] = 1;
	dict["Blue"] = 2;
	dict["Magenta"] = 3;
	dict["Green"] = 4;
	dict["Cyan"] = 5;
	int n;
	while(cin>>n,n){
		vector<Dice> v(n);
		rep(i,n){
			v[i] = Dice(6);
			rep(j,6){
				string s;
				cin>>s;
				v[i][j] = dict[s];
			}
		}
		int cnt = 0;
		rep(i,n){
			bool ok = true;
			rep(j,i){
				vector<Dice> tmp = generate_all(v[j]);
				rep(k, tmp.size()){
					if(tmp[k] == v[i]) ok = false;
				}
			}
			if(ok){
				cnt++;
			}
		}
		cout << n - cnt << endl;
	}
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}