#include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

const int N=30;
vector<int>rot_l(vector<int>&v) {
	int k=v[1];
	v[1]=v[2];
	v[2]=v[4];
	v[4]=v[3];
	v[3]=k;
	return v;
}
vector<int>rot_u(vector<int>&v) {
	int k=v[1];
	v[1]=v[0];
	v[0]=v[4];
	v[4]=v[5];
	v[5]=k;
	return v;
}
vector<int>zero_top(vector<int>v) {
	{
		int x = find(v.begin(), v.end(), 0) - v.begin();
		if (x == 0) {

		}
		else if (x == 1) {
			rot_u(v);
			rot_u(v);
			rot_u(v);
		}
		else if (x == 5) {
			rot_u(v);
			rot_u(v);
			
		}
		else if (x == 4) {
			rot_u(v);
		}
		else if (x == 2) {
			rot_l(v); 
			rot_u(v);
			rot_u(v);
			rot_u(v);
		}
		else if (x == 3) {
			rot_l(v);
			rot_u(v);
		}
	}
	assert(v[0]==0);
	return v;
}
bool same(vector<int>l, vector<int>r) {
	l=zero_top(l);
	r=zero_top(r);
	for (int i = 0; i < 4; ++i) {
		if(l==r)return true;
		rot_l(l);
	}
	return false;
}

int main() {
	while (true) {
		int N; cin >> N;if(!N)break;
		vector<vector<int>>cubes;
		for (int i = 0; i < N; ++i) {
			string color_st = "MRYBGC";
			vector<int>v;
			for (int j = 0; j < 6; ++j) {
				string st; cin >> st;
				int k = color_st.find(st[0]);
				v.push_back(k);
			}
			bool ok=true;
			for (auto cube : cubes) {
				if(same(cube,v))ok=false;
			}
			if (ok) {
				cubes.emplace_back(v);
			}
		}
		cout<<N-cubes.size()<<endl;
	}
	
	return 0;
}
