#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
class cube {
public:
	string color;
	void input() {
		string str;
		color = "";
		for (int i = 0; i < 6; ++i) {
			cin >> str;
			color += str[0];
		}
	}
};
int turn[3][4]{ {1,2,4,3},{0,1,5,4},{0,2,5,3} };
void turncube(cube&, int);
bool check(cube, cube);
int main() {
	int num;
	while (cin >> num&&num != 0) {
		vector<cube> line(num);
		for (int i = 0; i < num; ++i) {
			line[i].input();
		}
		int ans = 0;
		for (int i = 0; i < line.size() - 1; ++i) {
			for (int j = i+1; j < line.size(); ++j) {
				if (check(line[i], line[j])) {
					swap(line[j], line.back());
					line.pop_back();
					++ans;
					--j;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

void turncube(cube &a, int num) {
	for (int i = 0; i < 3; ++i) {
		swap(a.color[turn[num][i]], a.color[turn[num][i + 1]]);
	}
}

bool check(cube a, cube b) {
	int num[] = { 1,1,1,1,2,1,1,1,1 };
	int i = 0;
	while (a.color[0] != b.color[0]) {
		turncube(a, num[i++]);
	}
	for (int i = 0; i < 4; ++i) {
		bool flag = true;
		turncube(a, 0);
		for (int j = 0; j < 6; ++j) {
			if (a.color[j] != b.color[j]) {
				flag = false;
			}
		}
		if (flag)return true;
	}
	return false;
}