#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;
			}
		}
	}
	
	for(int i = 1; i <= 6 && f; i++){
		if(s1[i] != s3[i]){
			f = false;
		}
	}
	
	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 && f[j]; j++){
				f[j] = func(s[i], s[j]);
			}
		}
		
		for(int i = 0; i < n; i++){
			if(f[i]){
				c++;
			}
		}
		
		cout << (n-c) << endl;
	}
	
	return 0;
}