#include<iostream>
#include<vector>
#include<map>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector<int> D;
int R[][6]={{0,3,1,4,2,5},{2,1,5,0,4,3},{4,0,2,3,5,1}};
void rot(D &d,int a){
	D r(6); rep(i,6)r[i]=d[R[a][i]]; d=r;
}
int main(){
	map<char,int> M; M['R']=0,M['Y']=1,M['B']=2,M['M']=3,M['G']=4,M['C']=5;
	int n;
	while(cin>>n,n){
		set<D> s,alld; char b[9];
		rep(ii,n){
			D d(6),t(6);
			alld.clear();
			rep(jj,6)cin>>b,d[jj]=M[b[0]];
			rep(i,4)rep(j,4)rep(k,4){
				t=d;
				rep(p,i)rot(t,0);rep(q,j)rot(t,1);rep(r,k)rot(t,2);
				alld.insert(t);
			}
			s.insert(*alld.begin());
		}
		cout<<n-s.size()<<endl;
	}
	return 0;
}