#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<set>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

class S{
	public:
	vector<int> a;
	S(){}
	S(vector<int> a):a(a){}
	void x(){
		int tmp = a[0];
		a[0]=a[4];
		a[4]=a[5];
		a[5]=a[1];
		a[1]=tmp;
	}
	void y(){
		int tmp = a[1];
		a[1]=a[2];
		a[2]=a[4];
		a[4]=a[3];
		a[3]=tmp;
	}
	void z(){
		int tmp = a[0];
		a[0]=a[2];
		a[2]=a[5];
		a[5]=a[3];
		a[3]=tmp;
	}
};

int main(){
	while(1){
		int n;
		cin>>n;
		if(n==0)break;
		
		map<vector<int>,int> memo;
		
		rep(i,n){
			vector<int> b;
			rep(j,6){
				string a;
				cin>>a;
				switch(a[0]){
					case 'C':b.push_back(0);break;
					case 'Y':b.push_back(1);break;
					case 'R':b.push_back(2);break;
					case 'M':b.push_back(3);break;
					case 'G':b.push_back(4);break;
					case 'B':b.push_back(5);break;
				}
			}
			
			S si = S(b);
			rep(j,4){
				si.x();
				rep(k,4){
					si.y();
					rep(p,4){
						si.z();
						memo[si.a]=1;
					}
				}
			}
		}
		
		printf("%d\n",n-(memo.size()/24));
		
	}
}