// 2011/07/21 Tazoe

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

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++){
			for(int j=0; j<6; j++)
				cout << c[i][j] << ' ';
			cout << endl;
		}
*/
		for(int i=0; i<n; i++){
			if(c[i][0]>c[i][5]){
				int tmp = c[i][0];
				c[i][0] = c[i][5];
				c[i][5] = tmp;

				tmp = c[i][2];
				c[i][2] = c[i][3];
				c[i][3] = tmp;
			}
			if(c[i][1]>c[i][4]){
				if(c[i][2]<c[i][3]){
					int tmp = c[i][1];
					c[i][1] = c[i][2];
					c[i][2] = c[i][4];
					c[i][4] = c[i][3];
					c[i][3] = tmp;
				}
				else{
					int tmp = c[i][1];
					c[i][1] = c[i][4];
					c[i][4] = tmp;

					tmp = c[i][2];
					c[i][2] = c[i][3];
					c[i][3] = tmp;
				}
			}
			if(c[i][2]>c[i][3]){
				int tmp = c[i][2];
				c[i][2] = c[i][1];
				c[i][1] = c[i][3];
				c[i][3] = c[i][4];
				c[i][4] = tmp;
			}
			if(c[i][1]==1){
				int tmp = c[i][1];
				c[i][1] = c[i][2];
				c[i][2] = c[i][0];
				c[i][0] = tmp;
				tmp = c[i][4];
				c[i][4] = c[i][3];
				c[i][3] = c[i][5];
				c[i][5] = tmp;
			}
			if(c[i][2]==1){
				int tmp = c[i][2];
				c[i][2] = c[i][1];
				c[i][1] = c[i][0];
				c[i][0] = tmp;
				tmp = c[i][3];
				c[i][3] = c[i][4];
				c[i][4] = c[i][5];
				c[i][5] = tmp;
			}
		}
/*
		for(int i=0; i<n; i++){
			for(int j=0; j<6; j++)
				cout << c[i][j] << ' ';
			cout << endl;
		}
*/
		int d[30];
		for(int i=0; i<n; i++)
			d[i] = ((((c[i][0]*10+c[i][1])*10+c[i][2])*10+c[i][3])*10+c[i][4])*10+c[i][5];

		for(int i=0; i<n-1; i++)
			for(int j=i+1; j<n; j++)
				if(d[i]>d[j]){
					int tmp = d[i];
					d[i] = d[j];
					d[j] = tmp;
				}
/*
		for(int i=0; i<n; i++)
			cout << d[i] << endl;
*/
		int m = n;
		for(int i=0; i<n-1; i++)
			if(d[i]==d[i+1])
				m--;

		cout << n-m << endl;
	}

	return 0;
}