#include <bits/stdc++.h>
using namespace std;

vector<string>V;
int find(string s){
	for(int i=0;i<V.size();i++)if(V[i]==s)return 1;
	return 0;
}

int d[3][6]={
	{3,0,1,2,4,5},
	{4,1,5,3,2,0},
	{0,4,2,5,3,1}
};
string rot(string s,int n){
	string S=s;
	for(int i=0;i<6;i++)S[i]=s[d[n][i]];
	return S;
}
int step[12]={0,1,0,1,0,1,0,1,2,0,2,0};
int STEP[12]={4,1,4,1,4,1,4,1,1,4,2,4};

string cng(string s){
	string S=s;
	S[0]=s[3];
	S[1]=s[0];
	S[2]=s[2];
	S[3]=s[5];
	S[4]=s[4];
	S[5]=s[1];
	return S;
}

int main(){
	
	for(int n;cin>>n,n;){
		V.clear();
		for(int i=0;i<n;i++){
			string s="",t;
			for(int j=0;j<6;j++){
				cin>>t;
				s+=t[0];
			}
			s=cng(s);
			int f=1;
			for(int j=0;j<12;j++){
				for(int k=0;k<STEP[j];k++){
					s=rot(s,step[j]);
					if(find(s))f=0;
				}
			}
			if(f)V.push_back(s);
		}
		cout<<n-V.size()<<endl;
	}
	
	
	return 0;
}
