#include <stdio.h>
#include <set>

using namespace std;

set<unsigned int> hashList;

int make_face(char input)
{
	int ret;
	
	switch (input){
	  case 'R': ret = 0; break;
	  case 'G': ret = 1; break;
	  case 'B': ret = 2; break;
	  case 'C': ret = 3; break;
	  case 'M': ret = 4; break;
	  case 'Y': ret = 5; break;
	}
	
	return (ret);
}

unsigned int make_hash(int cube[])
{
	int ret;
	
	ret = 0;
	for (int i = 0; i < 6; i++){
		ret = ret * 6 + cube[i];
	}
	
	return (ret);
}

int rote_x(int cube[])
{
	int temp;
	
	temp = cube[1];
	cube[1] = cube[3];
	cube[3] = cube[4];
	cube[4] = cube[2];
	cube[2] = temp;
	
	return (0);
}

int rote_y(int cube[])
{
	int temp;
	
	temp = cube[0];
	cube[0] = cube[3];
	cube[3] = cube[5];
	cube[5] = cube[2];
	cube[2] = temp;
	
	return (0);
}

int rote_z(int cube[])
{
	int temp;
	
	temp = cube[0];
	cube[0] = cube[4];
	cube[4] = cube[5];
	cube[5] = cube[1];
	cube[1] = temp;
	
	return (0);
}

int main(void)
{
	int cube[6];
	int ans;
	int n;
	
	while (1){
		hashList.clear();
		
		scanf("%d", &n);
		
		if (n == 0){
			 break;
		}
		
		ans = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < 6; j++){
				char str[20];
				
				scanf("%s", str);
				cube[j] = make_face(str[0]);
			}
			if (hashList.find(make_hash(cube)) != hashList.end()){
				ans++;
				continue;
			}
			for (int i = 0; i < 4; i++){
				for (int j = 0; j < 4; j++){
					for (int k = 0; k < 4; k++){
						hashList.insert(make_hash(cube));
						rote_x(cube);
					}
					rote_y(cube);
				}
				rote_z(cube);
			}
		}
		
		printf("%d\n", ans);
	}
	
	return (0);
}