#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool func(string s1[7], string s2[7])
{
	bool f = true, fs[7];
	string s3[7];
	fill(fs, fs+7, true);
	
	for(int i = 1; i <= 3 && f; i++){
		for(int j = 1; j <= 6 && f; j++){
			if(s1[i] == s2[j]){
				if(!fs[j]){
					f = false;
				} else {
					fs[j] = fs[7-j] = false;
					s3[i] = s2[j];
					s3[7-i] = s2[7-j];
				}
				break;
			}
		}
	}
	/*
	cout << endl;
	cout << "s1 : ";
	for(int i = 1; i <= 6; i++){
		cout << s1[i] << " ";
	}
	cout << endl;
	cout << "s2 : ";
	for(int i = 1; i <= 6; i++){
		cout << s2[i] << " ";
	}
	cout << endl;
	cout << "s3 : ";
	for(int i = 1; i <= 6; i++){
		if(s3[i].size() == 0){
			cout << "NONE" << " ";
			continue;
		}
		cout << s3[i] << " ";
	}
	cout << endl;
	*/
	for(int i = 1; i <= 6 && f; i++){
		if(s1[i] != s3[i]){
			f = false;
		}
	}
	/*
	if(f){
		cout << "matching" << endl;
	} else {
		cout << "not matching" << endl;
	}
	cout << endl;
	*/
	return !f;
}

int main()
{
	int n, c;
	string s[31][7];
	bool f[31];
	
	while(cin>>n&&n){
		for(int i = 0; i < n; i++){
			for(int j = 1; j <= 6; j++){
				cin >> s[i][j];
			}
		}
		c = 0;
		fill(f, f+31, true);
		
		for(int i = 0; i < n; i++){
			for(int j = i+1; j < n; j++){
				f[j] = func(s[i], s[j]) & f[j];
			}
		}
		
		for(int i = 0; i < n; i++){
			if(f[i]){
				c++;
			}
		}
		
		cout << (n-c) << endl;
	}
	
	return 0;
}