#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

vector<string> CubeInfo[30];
string StrTmp[6];
int N;
bool SameFlag[30];
//top front right left back bottom
int RotateBase[24][6] = {
	{ 0,1,2,3,4,5 },{},{},{},
	{ 1,5,2,3,0,4 },{},{},{},
	{ 2,1,5,0,4,3 },{},{},{},
	{ 3,1,0,5,4,2 },{},{},{},
	{ 4,0,2,3,5,1 },{},{},{},
	{ 5,4,2,3,1,0 },{},{},{},
};


void fullRotateBase() {
	int tmp[6], num;

	for (int i = 0; i < 21; i += 4) {
		for (int j = 0; j < 6; ++j) {
			tmp[j] = RotateBase[i][j];
		}
		for (int k = 1; k < 4; ++k) {
			num = tmp[1];
			tmp[1] = tmp[3];
			tmp[3] = tmp[4];
			tmp[4] = tmp[2];
			tmp[2] = num;
			for (int j = 0; j < 6; ++j) {
				RotateBase[i + k][j] = tmp[j];
			}
		}
	}
	/*for (int i = 0; i < 24; ++i) {
		for (int j = 0; j < 6; ++j) {
			cout << RotateBase[i][j] + 1 << " ";
		}
		cout << endl;
	}
	cout << endl;*/
}

bool judgeSame(int i, int j) {
	string tmp[6];
	bool flag;

	for (int id = 0; id < 24; ++id) {
		for (int k = 0; k < 6; ++k) {
			tmp[k] = CubeInfo[j][RotateBase[id][k]];
		}
		for (int k = 0; k < 6; ++k) {
			if (CubeInfo[i][k] != tmp[k]) {
				flag = false;
				break;
			}
			flag = true;
		}
		if (flag) {
			return true;
		}
	}
	return false;
}

int solve() {
	int cnt;

	cnt = 0;
	for (int i = 0; i < N; ++i) {
		if (SameFlag[i]) {
			continue;
		}
		for (int j = i + 1; j < N; ++j) {
			if (judgeSame(i, j)) {
				SameFlag[j] = true;
				++cnt;
			}
		}
	}
	return cnt;
}

int main() {
	string str;

	fullRotateBase();
	while (cin >> N, N) {
		memset(SameFlag, false, sizeof(SameFlag));
		for (int i = 0; i < N; ++i) {
			CubeInfo[i].clear();
			for (int j = 0; j < 6; ++j) {
				cin >> str;
				CubeInfo[i].push_back(str);
			}
		}
		cout << solve() << endl;
	}

	return 0;
}
