#include<iostream>
#include<vector>
#include<algorithm>

int colorToInt(std::string s){
	const	char cs[] = {'C', 'Y', 'R', 'M', 'G', 'B'};
	for(int i=0;i<6;i++){
		if(s[0] == cs[i]){
			return i;
		}
	}
}

int main(){
	int n;
	while(std::cin >> n, n){
		std::vector<int> cube_v;
		int cyan_pos;

		for(int i=0;i<n;i++){
			int cs[6];
			for(int j=0;j<6;j++){
				std::string c;
				std::cin >> c;
				cs[j] = colorToInt(c);
			}

			std::swap(cs[3], cs[5]);
			for(int j=0;j<6;j++){
				if(cs[j] == 0){
					cyan_pos = j;
				}
			}

			int cube = cs[(cyan_pos+3)%6] << 6,
				vs[] = {(cs[(cyan_pos+1)%6] << 3) + cs[(cyan_pos+4)%6],
					(cs[(cyan_pos+2)%6] << 3) + cs[(cyan_pos+5)%6]};

			for(int j=0;j<2;j++){
				if(std::find(cube_v.begin(), cube_v.end(), cube+vs[j]) == cube_v.end()){
					cube_v.push_back(cube+vs[j]);
				}
			}
		}

		std::cout << (n-cube_v.size()/2) << std::endl;
	}
}