#include<iostream>
#include<string>
using namespace std;
int Dice[6][6] = { { 1,5,2,3,0,4 },{ 4,0,2,3,5,1 },{ 3,1,0,5,4,2 },{ 2,1,5,0,4,3 },{ 0,2,4,1,3,5 },{ 0,3,1,4,2,5 } }, power[6] = { 1,3,9,27,81,243 };
struct Saikoro { int x[6]; }; Saikoro T[30]; int n, vis[30]; char F[7] = "CYRMGB"; bool G[30][30];
bool bfs(int p, int q) {
	for (int i = 0; i < 729; i++) {
		int bit[6]; for (int j = 0; j < 6; j++) { bit[j] = (i / power[j]) % 3; }
		Saikoro U = T[p];
		for (int j = 0; j < 6; j++) {
			for (int k = 0; k <= bit[j]; k++) {
				Saikoro V = U; for (int l = 0; l < 6; l++)V.x[l] = U.x[Dice[j][l]]; U = V;
			}
		}
		bool OK = true; for (int j = 0; j < 6; j++) { if (T[q].x[j] != U.x[j])OK = false; }
		if (OK == true)return true;
	}
	return false;
}
int main() {
	while (true) {
		for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++)G[i][j] = false; vis[i] = 0; }
		cin >> n; if (n == 0)break; for (int i = 0; i < n; i++) { for (int j = 0; j < 6; j++) { string S; cin >> S; for (int k = 0; k < 6; k++) { if (S[0] == F[k])T[i].x[j] = k; } } }
		for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j)continue; G[i][j] = bfs(i, j); } }
		int cnt = 0; for (int i = 0; i < n; i++) { if (vis[i] != 0)continue; cnt++; vis[i] = cnt; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (vis[k] != cnt)continue; for (int l = 0; l < n; l++) { if (G[k][l] == true)vis[l] = cnt; } } } }
		cout << n - cnt << endl;
	}
	return 0;
}