#include <bits/stdc++.h>
using namespace std;

struct Dice {
	vector<int> val;
	Dice(){ val.resize(6); }
	bool operator == (const Dice &t) const {
		return val == t.val;
	}
	bool operator != (const Dice &t) const {
		return val != t.val;
	}
	bool operator < (const Dice &t) const {
		return val < t.val;
	}
	void rotate_right(){
		int tmp = val[1];
		val[1] = val[2];
		val[2] = val[4];
		val[4] = val[3];
		val[3] = tmp;
	}
	void roll_front(){
		int tmp = val[0];
		val[0] = val[1];
		val[1] = val[5];
		val[5] = val[4];
		val[4] = tmp;
	}
	void roll_right(){
		int tmp = val[0];
		val[0] = val[3];
		val[3] = val[5];
		val[5] = val[2];
		val[2] = tmp;
	}
	vector<Dice> all_pattern(){
		vector<Dice> res;
		Dice d = *this;
		for (int i = 0; i < 6; i++){
			for (int j = 0; j < 4; j++){
				res.push_back(d);
				d.rotate_right();
			}
			if (i == 0) d.roll_front();
			else if (i < 5) d.roll_right();
			else d.roll_front();
		}
		return res;
	}
};

struct UnionFind {
	vector<int> parent;
	
	UnionFind(int n){
		parent.resize(n);
		for (int i = 0; i < n; i++){
			parent[i] = i;
		}
	}
	
	int find(int x){
		if (parent[x] == x) return x;
		return parent[x] = find(parent[x]);
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}
	
	bool unite(int x, int y){
		x = find(x);
		y = find(y);
		if (x == y) return false;
		parent[x] = y;
		return true;
	}
};

map<string, int> ctoi;
int n;
vector<Dice> dices;

bool check(const vector<Dice> &A, const vector<Dice> &B)
{
	for (int i = 0; i < A.size(); i++){
		for (int j = 0; j < B.size(); j++){
			if (A[i] == B[j]) return true;
		}
	}
	return false;
}

void solve()
{
	vector<vector<Dice> > pats;
	UnionFind uf(n);
	
	for (int i = 0; i < n; i++){
		pats.push_back(dices[i].all_pattern());
	}
	
	int res = 0;
	
	for (int i = 0; i < n; i++){
		for (int j = i + 1; j < n; j++){
			vector<Dice> &A = pats[i];
			vector<Dice> &B = pats[j];
			if (check(A, B)){
				res += uf.unite(i, j);
				continue;
			}
		}
	}
	
	printf("%d\n", res);
}

int main()
{
	ctoi["Red"] = 0;
	ctoi["Yellow"] = 1;
	ctoi["Blue"] = 2;
	ctoi["Magenta"] = 3;
	ctoi["Green"] = 4;
	ctoi["Cyan"] = 5;
	
	while (scanf("%d", &n), n){
		dices.clear();
		for (int i = 0; i < n; i++){
			Dice d;
			for (int j = 0; j < 6; j++){
				string str;
				cin >> str;
				d.val[j] = ctoi[str];
			}
			dices.push_back(d);
		}
		solve();
	}
	
	return 0;
}