#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>

using namespace std;

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

#define f first
#define s second

int main(){
	while(1){
		int n;
		cin>>n;
		
		if(n==0)break;
		
		set<vector<int> > ch;
		
		rep(pp,n){
			vector<int> a;
			rep(j,6){
				string sa;
				cin>>sa;
				switch(sa[0]){
					case 'C':a.push_back(0);break;
					case 'Y':a.push_back(1);break;
					case 'R':a.push_back(2);break;
					case 'M':a.push_back(3);break;
					case 'G':a.push_back(4);break;
					case 'B':a.push_back(5);break;
				}
			}
			
			rep(i,4){
				int temp=a[0];
				a[0]=a[4];
				a[4]=a[5];
				a[5]=a[1];
				a[1]=temp;
				
				rep(j,4){
					temp=a[0];
					a[0]=a[2];
					a[2]=a[5];
					a[5]=a[3];
					a[3]=temp;
					
					rep(k,4){
						temp=a[1];
						a[1]=a[2];
						a[2]=a[4];
						a[4]=a[3];
						a[3]=temp;
						
						ch.insert(a);
					}
				}
			}
			
		}
		
		printf("%d\n",(n-ch.size()/24));
	}
}