// 2011/08/01 Tazoe

#include <iostream>
#include <string>
using namespace std;

void roll01(int c[6])
{
	int tmp = c[0];
	c[0] = c[1];
	c[1] = c[5];
	c[5] = c[4];
	c[4] = tmp;
}

void roll02(int c[6])
{
	int tmp = c[0];
	c[0] = c[2];
	c[2] = c[5];
	c[5] = c[3];
	c[3] = tmp;
}

void roll12(int c[6])
{
	int tmp = c[1];
	c[1] = c[2];
	c[2] = c[4];
	c[4] = c[3];
	c[3] = tmp;
}

void rotate(int c[6])
{
	if(c[0]==1){
		;
	}
	else if(c[1]==1){
		roll01(c);
	}
	else if(c[2]==1){
		roll02(c);
	}
	else if(c[3]==1){
		roll02(c);
		roll02(c);
		roll02(c);
	}
	else if(c[4]==1){
		roll01(c);
		roll01(c);
		roll01(c);
	}
	else if(c[5]==1){
		roll02(c);
		roll02(c);
	}

	if(c[1]==2){
		;
	}
	else if(c[2]==2){
		roll12(c);
	}
	else if(c[4]==2){
		roll12(c);
		roll12(c);
	}
	else if(c[3]==2){
		roll12(c);
		roll12(c);
		roll12(c);
	}
	else if(c[1]==3){
		;
	}
	else if(c[2]==3){
		roll12(c);
	}
	else if(c[4]==3){
		roll12(c);
		roll12(c);
	}
	else if(c[3]==3){
		roll12(c);
		roll12(c);
		roll12(c);
	}
}

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int C[30][6];
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++){
				string str;
				cin >> str;
				if(str=="Red")
					C[i][j] = 1;
				else if(str=="Yellow")
					C[i][j] = 2;
				else if(str=="Blue")
					C[i][j] = 3;
				else if(str=="Magenta")
					C[i][j] = 4;
				else if(str=="Green")
					C[i][j] = 5;
				else if(str=="Cyan")
					C[i][j] = 6;
			}
		}

		for(int i=0; i<n; i++)
			rotate(C[i]);

		for(int i=0; i<n-1; i++)
			for(int j=i+1; j<n; j++)
				if(C[i][1]>C[j][1]
				 ||(C[i][1]==C[j][1]&&C[i][2]>C[j][2])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]>C[j][3])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]==C[j][3]&&C[i][4]>C[j][4])
				 ||(C[i][1]==C[j][1]&&C[i][2]==C[j][2]&&C[i][3]==C[j][3]&&C[i][4]==C[j][4]&&C[i][5]>C[j][5]))
					for(int k=0; k<6; k++){
						int tmp = C[i][k];
						C[i][k] = C[j][k];
						C[j][k] = tmp;
					}

		int m = n;
		for(int i=0; i<n-1; i++){
			bool flg = true;
			for(int j=0; j<6; j++)
				if(C[i][j]!=C[i+1][j]){
					flg = false;
					break;
				}
			if(flg)
				m--;
		}
		cout << n-m << endl;
	}

	return 0;
}