#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <tuple>
#include <set>
#include <string>

using namespace std;

#define int long long

signed main() {
	map<char, int> colorID;
	colorID.emplace('R', 0);
	colorID.emplace('Y', 1);
	colorID.emplace('B', 2);
	colorID.emplace('M', 3);
	colorID.emplace('G', 4);
	colorID.emplace('C', 5);

	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;

		vector<vector<int>> colors(n, vector<int>(6));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 6; j++) {
				string s;
				cin >> s;
				colors[i][j] = colorID[s[0]];
			}

			while (colors[i][0] != 0) {
				int zeroI = 0;
				for (; zeroI < 6; zeroI++) {
					if (colors[i][zeroI] == 0)break;
				}

				if (zeroI == 2 || zeroI == 3 || zeroI == 5) {
					swap(colors[i][3], colors[i][0]);
					swap(colors[i][0], colors[i][2]);
					swap(colors[i][2], colors[i][5]);
				}
				else {
					swap(colors[i][4], colors[i][0]);
					swap(colors[i][0], colors[i][1]);
					swap(colors[i][1], colors[i][5]);
				}
			}

			int sideMin = min(
				min(colors[i][1], colors[i][2]),
				min(colors[i][3], colors[i][4]));
			while (colors[i][1] != sideMin) {
				swap(colors[i][1], colors[i][2]);
				swap(colors[i][2], colors[i][4]);
				swap(colors[i][4], colors[i][3]);
			}
		}

		set<vector<int>> color_set;
		for (int i = 0; i < n; i++)color_set.emplace(colors[i]);

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