#include <iostream>
#include <string>
#include <vector>
using namespace std;

int same[][4] = {
	{1, 2, 4, 3},
	{5, 2, 0, 3},
	{1, 5, 4, 0},
	{1, 0, 4, 5},
	{0, 2, 5, 3},
	{1, 3, 4, 2},
};

int s2i(string s) {
	if(s == "Red")
		return 0;
	if(s == "Yellow")
		return 1;
	if(s == "Blue")
		return 2;
	if(s == "Magenta")
		return 3;
	if(s == "Green")
		return 4;
	if(s == "Cyan")
		return 5;
	return -1;
}

bool eq(vector<int> a, vector<int> b) {
	int indx0, indx1;
	for(int i = 0; i < 6; i++) {
		if(a[i] == 0)
			indx0 = i;
		if(b[i] == 0)
			indx1 = i;
	}

	for(int i = 0; i < 4; i++) {
		bool flg = true;
		for(int j = 0; j < 4; j++) {
			flg &= a[same[indx0][j]] == b[same[indx1][(j + i) % 4]];
		}
		if(flg)
			return true;
	}

	return false;
}

int main() {
	int n;
	while(cin >> n, n) {
		vector<vector<int> > v;
		for(int i = 0; i < n; i++) {
			vector<int> tmp;
			for(int j = 0; j < 6; j++) {
				string s;
				cin >> s;
				tmp.push_back(s2i(s));
			}
			bool flg = true;
			for(int j = 0; j < v.size(); j++) {
				if(eq(v[j], tmp)) {
					flg = false;
				}
			}
			if(flg)
				v.push_back(tmp);
		}

		cout << n - v.size() << endl;
	}
}