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

struct datum {
	char str[6];
};

datum data[30];

char now[6];
char temp[6];

void rotate() {
	char temp;
	temp = now[0];
	now[0] = now[2];
	now[2] = now[5];
	now[5] = now[3];
	now[3] = temp;
}

void spin () {
	char temp;
	temp = now[1];
	now[1] = now[2];
	now[2] = now[4];
	now[4] = now[3];
	now[3] = temp;
}

int compare ( const void * a, const void * b ) {
	const datum x = *((datum*)a);
	const datum y = *((datum*)b);

	for ( int i=0; i<6; i++ ) {
		if ( x.str[i] != y.str[i] )
			return x.str[i] - y.str[i];
	}
	return 0;
}


int main () {
	while ( true ) {
		int n;
		cin >> n;

		if ( n == 0 ) 
			break;

		string str;
		for ( int i=0; i<n; i++ ) {
			for ( int j=0; j<6; j++ ) {
				
				cin >> str;
				now[j] = str[0];
			}

			if ( now[1] == 'B' || now[4] == 'B' ) {
				spin();
			}
			while ( now[0] != 'B' ) {
					rotate();
			}

			if ( now[5] == 'Y' ) {
				while ( now[1] != 'C' ) {
					spin();
				}
			}
			else {
				while ( now[1] != 'Y' ) {
					spin();
				}
			}

			for ( int j=0; j<6; j++ ) {
				data[i].str[j] = now[j];
			}
		}

		qsort ( data, n, sizeof(datum), compare);

		int count = 0;

		for ( int i=0; i<n-1; i++ ) {
			bool hit = true;
			for ( int j=0; j<6; j++ ) {
				if ( data[i].str[j] != data[i+1].str[j] ) {
					hit = false;
					break;
				}
			}
			if ( hit ) 
				count ++;
		}

		cout << count << endl;
	}
}

		