#include<bits/stdc++.h>
#include<set>
using namespace std;
#define pb push_back
int N;
int co(int i,int j,int k){
	if(i>j)swap(i,j);
	if(i>k)swap(i,k);
	if(j>k)swap(j,k);
	return i*36+j*6+k;
}
int main()
{
	while(true){
		scanf("%d",&N);
		if(N==0)return 0;
		int X[6];
		set<vector<int> >S;
		for(int i=0;i<N;i++){
			for(int j=0;j<6;j++){
				string SS;
				cin >> SS;
				if(SS=="Cyan")X[j]=0;
				if(SS=="Yellow")X[j]=1;
				if(SS=="Red")X[j]=2;
				if(SS=="Magenta")X[j]=3;
				if(SS=="Green")X[j]=4;
				if(SS=="Blue")X[j]=5;
			}
			vector<int>XX;
			XX.pb(co(X[0],X[1],X[2]));
			XX.pb(co(X[1],X[2],X[5]));
			XX.pb(co(X[0],X[1],X[3]));
			XX.pb(co(X[1],X[3],X[5]));
			XX.pb(co(X[0],X[4],X[2]));
			XX.pb(co(X[4],X[2],X[5]));
			XX.pb(co(X[0],X[4],X[3]));
			XX.pb(co(X[4],X[3],X[5]));
			sort(XX.begin(),XX.end());
			S.insert(XX);
		}
		printf("%d\n",N-(int)S.size());
	}
}