#include <stdio.h>

int translate(char Head){
	switch(Head){
	case 'R':return 0;
	case 'Y':return 1;
	case 'B':return 2;
	case 'M':return 3;
	case 'G':return 4;
	case 'C':return 5;
	}
	return -1; //must not reach here
}

class Dice{
public:
	Dice(){
		num_of_data = 0;
	};

	void roll(char dst){
		switch(dst){
		case 'E':
			setNumber(number[3],number[1],number[0],number[5],number[4],number[2]);
			break;
		case 'N':
			setNumber(number[1],number[5],number[2],number[3],number[0],number[4]);
			break;
		case 'S':
			setNumber(number[4],number[0],number[2],number[3],number[5],number[1]);
			break;
		case 'W':
			setNumber(number[2],number[1],number[5],number[0],number[4],number[3]);
			break;
		}
	};

	bool isNewPattern(int inputed[]){
		char order[25] = "NNNNWNNNWNNNENNNENNNWNNN";

		for(int i = 0; i < num_of_data; i++){
			for(int k = 0; k < 6; k++)number[k] = data_bank[i][k];
			for(int k = 0; k < 24; k++){
				roll(order[k]);
				for(int j = 0; j < 6; j++){
					if(inputed[j] != number[j])
						break;
					if(j == 5)
						return false;
				}
			}
		}
		return true;
	}

	void init(){
		num_of_data = 0;
	}

	int getNumOfData(){
		return num_of_data;
	}

	void addData(int table[6]){
		data_bank[num_of_data][0] = table[0];
		data_bank[num_of_data][1] = table[1];
		data_bank[num_of_data][2] = table[2];
		data_bank[num_of_data][3] = table[3];
		data_bank[num_of_data][4] = table[4];
		data_bank[num_of_data][5] = table[5];
		num_of_data++;
	}

private:

	void setNumber(int n0,int n1,int n2,int n3,int n4,int n5){
		number[0] = n0;
		number[1] = n1;
		number[2] = n2;
		number[3] = n3;
		number[4] = n4;
		number[5] = n5;
	}
private:
	int number[6];
	int data_bank[30][6];
	int num_of_data;
};

int main(){
	int n,tmp[6];
	char c1[10],c2[10],c3[10],c4[10],c5[10],c6[10];
	Dice dice;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		dice.init();

		for(int i = 0; i < n; i++){
			scanf("%s %s %s %s %s %s",c1,c2,c3,c4,c5,c6);
			tmp[0] = translate(c1[0]);
			tmp[1] = translate(c2[0]);
			tmp[2] = translate(c3[0]);
			tmp[3] = translate(c4[0]);
			tmp[4] = translate(c5[0]);
			tmp[5] = translate(c6[0]);

			if(dice.isNewPattern(tmp)){
				dice.addData(tmp);
			}
		}
		printf("%d\n",n-dice.getNumOfData());

	}
}
