#include<iostream>
using namespace std;

#define rep(i,n)  for(int i=0; i<n; i++)

string rot1( string s ){
	string res = s;
	res[0] = s[1];
	res[1] = s[5];
	res[5] = s[4];
	res[4] = s[0];
	return res;
}

string rot2( string s ){
	string res = s;
	res[0] = s[2];
	res[2] = s[5];
	res[5] = s[3];
	res[3] = s[0];
	return res;
}

string rot3( string s ){
	string res = s;
	res[1] = s[2];
	res[2] = s[4];
	res[4] = s[3];
	res[3] = s[1];
	return res;
}

int main(){
	int n;
	while( cin >> n, n ){
		string obj[30];
		rep(i,n){
			rep(j,6){
				string in;
				cin >> in;
				obj[i] += in[0];
			}
		}
		int cnt = 0;
		rep(i,n){
			for(int j=i+1; j<n; j++){
				bool found = false;
				rep(k,6){
					rep(l,4){
						if( obj[i] == obj[j] ){
							found = true;
							break;
						}
						obj[j] = rot3(obj[j]);
					}
					if(found)	break;
					if(k%2)	obj[j] = rot1(obj[j]);
					else	obj[j] = rot2(obj[j]);
				}
				if(found)	cnt++;
			}
		}
		cout << cnt << endl;
	}
	return 0;
}