#include <iostream>
#include <vector>
#include <set>
#include <map>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const int trans[][6] = 	{
									{ 0, 1, 2, 3, 4, 5 },	// no rotation 
									{ 2, 1, 5, 0, 4, 3 },	// left rotation
									{ 3, 1, 0, 5, 4, 2 }, 	// right rotation
									{ 1, 5, 2, 3, 0, 4 },	// top rotation
									{ 4, 0, 2, 3, 5, 1 },	// bottom rotation
									{ 0, 2, 4, 1, 3, 5 }	// front and buttom fixed right 90 ratate 
								};
set <vector <char> > comb;

bool is_same (vector <char> s, int i, vector <char> & t )
{
	bool found = false;
	rep (j, 6 ){
		t[j] = s[trans[i][j]];
	} // end rep
	if (comb.count (t ) ){
		found = true;
	} // end if

	return found;
}

int main()
{
	const string color[6] = { "Red", "Cyan", "Yellow", "Blue", "Magenta", "Green" };
	map<string, char> C;
	rep (i, 6 )
		C[color[i]] = color[i][0];


	int n;
	while (cin >> n && n ){
		comb.clear();
		vector <vector <char> > c(n, vector <char> (6 ) );
		rep (i, n ){
			rep (j, 6 ){
				string in;
				cin >> in;
				c[i][j] = C[in];
			} // end rep
		} // end rep
		for (int i = 0; i < n; i++ ){
			bool found = false;
			vector <char> t(6, 0 );
			rep (j, 5 ){
				if (is_same (c[i], j, t ) ){
					found = true;  
					break;
				} // end if
				vector <char> s = t;
				vector <char> r(6, 0 );
				rep (k, 3 )
				if (is_same (s, 5, r ) ){
					found = true;
					break;
				}else{
					s = r;
				} // end if
				vector <char> tt = t;			
				if (is_same (t, j, tt ) ){
					found = true; 
					break;
				} // end if
				s = tt;
				rep (k, 3 )
				if (is_same (s, 5, r ) ){
					found = true;
					break;
				}else{
					s = r;
				} // end if
			} // end rep
			if (!found ){
				comb.insert (c[i] );
			} // end if			
		} // end for
		int res = comb.size();
		cout << n - res << endl;
	} // end loop

		
	return 0;
}