#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int x[6][6] = { { 0,3,5,2,4,0 },{ 4,0,1,6,0,3 },{ 2,6,0,0,1,5 },{ 5,1,0,0,6,2 },{ 3,0,6,1,0,4 },{ 0,4,2,5,3,0 } };
vector<int>z[1000]; int Ty[1000], cnt;
bool solve(vector<int>p1, vector<int>p2) {
	int b[7] = { 0,0,0,0,0,0,0 }, maxn = 0, a[6] = { 0,0,0,0,0,0 };
	for (int i = 0; i < 6; i++) { b[p1[i]]++; b[p2[i]]++; }
	for (int i = 0; i < 7; i++)maxn = max(maxn, b[i]);
	if (maxn >= 6)return true;
	for (int i = 0; i < 6; i++) {
		for (int j = 0; j < 6; j++) {
			if (p1[j] == p2[i]) a[i] = j;
		}
	}
	if (x[a[0]][a[1]] == a[2] + 1) return true;
	return false;
}
int main() {
	while (true) {
		int n; cin >> n; if (n == 0)break;
		for (int i = 0; i < 1000; i++)z[i].clear();
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 6; j++) {
				string S; cin >> S;
				if (S[0] == 'C')z[i].push_back(1);
				if (S[0] == 'M')z[i].push_back(2);
				if (S[0] == 'Y')z[i].push_back(3);
				if (S[0] == 'R')z[i].push_back(4);
				if (S[0] == 'B')z[i].push_back(5);
				if (S[0] == 'G')z[i].push_back(6);
			}
		}
		cnt = 1; for (int i = 0; i < 1000; i++)Ty[i] = -1; Ty[0] = 1;
		for (int i = 1; i < n; i++) {
			for (int j = 0; j < i; j++) {
				if (solve(z[i], z[j]) == true)Ty[i] = Ty[j];
			}
			if (Ty[i] == -1) { cnt++; Ty[i] = cnt; }
		}
		cout << n - cnt << endl;
	}
	return 0;
}