#include <iostream>
#include <string>

using namespace std;

class Box{
public:
	string c1,c2,c3,c4,c5,c6;

	bool operator==( const Box& lhs ){
		return c1 == lhs.c1 && c2 == lhs.c2 && c3 == lhs.c3 && c4 == lhs.c4 && c5 == lhs.c5 && c6 == lhs.c6;
	}

	void rotatet(){
		string d1,d2;
		d1 = c1;
		c1 = c5;
		d2 = c2;
		c2 = d1;
		c5 = c6;
		c6 = d2;
	}

	void rotatey(){
		string d1,d2;
		d1 = c1;
		c1 = c3;
		d2 = c4;
		c4 = d1;
		c3 = c6;
		c6 = d2;
	}
};


int main( void )
{
	int n;
	Box box[31];
	bool check[31];

	while( cin >> n && n != 0 ){
		for( int i = 0;i < 31;i++ ) check[i] = false;
		for( int i = 0;i < n;i++ ){
			cin >> box[i].c1 >>  box[i].c2 >>  box[i].c3 >>  box[i].c4 >>  box[i].c5 >>  box[i].c6; 
		}

		int count = 0;

		for( int i = 0;i < n;i++ ){
			check[i] = true;
			for( int j = i;j < n;j++ ){
				if( check[j] == false ){
					for( int k = 0;k < 4;k++ ){
						box[j].rotatet();
						for( int n = 0;n < 4;n++ ){
							box[j].rotatey();
							if( box[i] == box[j] ){
								count++;
								check[j] = true;
								goto END;
							}
						}
					}
				}
END:;
			}
		}
		cout << count << endl;
	}
				

	return 0;
}