#include<iostream>

#define inf 1000000000

using namespace std;

int main(){
	while(1){
		int n;
		cin>>n;
		if(!n)break;
		int check[6][6][6][6][6][6]={0};
		for(int i=0;i<n;i++){
			int s[7];
			for(int j=1;j<=6;j++){
				string a;
				cin>>a;
				switch(a[0]){
					case 'C':s[j]=0;break;
					case 'Y':s[j]=1;break;
					case 'R':s[j]=2;break;
					case 'M':s[j]=3;break;
					case 'G':s[j]=4;break;
					case 'B':s[j]=5;break;
				}
			}
			int bin;
			for(int i=0;i<4;i++){
				bin=s[1];
				s[1]=s[5];
				s[5]=s[6];
				s[6]=s[2];
				s[2]=bin;
				for(int j=0;j<4;j++){
					bin=s[2];
					s[2]=s[3];
					s[3]=s[5];
					s[5]=s[4];
					s[4]=bin;
					check[s[1]][s[2]][s[3]][s[4]][s[5]][s[6]]=1;
				}
			}
			
			
					bin=s[1];
					s[1]=s[3];
					s[3]=s[6];
					s[6]=s[4];
					s[4]=bin;
				for(int j=0;j<4;j++){
					bin=s[2];
					s[2]=s[3];
					s[3]=s[5];
					s[5]=s[4];
					s[4]=bin;
					check[s[1]][s[2]][s[3]][s[4]][s[5]][s[6]]=1;
				}
					bin=s[1];
					s[1]=s[3];
					s[3]=s[6];
					s[6]=s[4];
					s[4]=bin;
					bin=s[1];
					s[1]=s[3];
					s[3]=s[6];
					s[6]=s[4];
					s[4]=bin;
				for(int j=0;j<4;j++){
					bin=s[2];
					s[2]=s[3];
					s[3]=s[5];
					s[5]=s[4];
					s[4]=bin;
					check[s[1]][s[2]][s[3]][s[4]][s[5]][s[6]]=1;
				}
		}
		
		int cont=0;
		for(int a=0;a<6;a++)
		for(int b=0;b<6;b++)
		for(int c=0;c<6;c++)
		for(int d=0;d<6;d++)
		for(int e=0;e<6;e++)
		for(int f=0;f<6;f++)
		cont+=check[a][b][c][d][e][f];
		
		printf("%d\n",n-cont/24);
	}
}