
// Trouble in Shinagawa's Artifacts
//  URL: http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=0198&lang=jp

#include <cstdio>
#include <cstring>
#include <vector>
#include <set>

// Ö
std::vector <int> rotate( std::vector <int> box, int a, int b, int c, int d );
std::vector <int> rotate_x( std::vector <int> box );
std::vector <int> rotate_y( std::vector <int> box );
std::vector <int> rotate_z( std::vector <int> box );

// ñ]
std::vector <int> rotate( std::vector <int> box, int a, int b, int c, int d )
{
	int tmp = box[a];
	box[a] = box[b];
	box[b] = box[c];
	box[c] = box[d];
	box[d] = tmp;

	return box;
}

// xÉñ]³¹éÖ
std::vector <int> rotate_x( std::vector <int> box )
{
	// xñ]
	// c1, c2, c3, c4, c5, c6 : default
	// c5, c1, c3, c4, c6, c2

	return rotate( box, 0, 4, 5, 1 );

}


// yÉñ]³¹éÖ
std::vector <int> rotate_y( std::vector <int> box )
{
	// yñ]
	// c1, c2, c3, c4, c5, c6 : default
	// c1, c4, c2, c5, c3, c6

	return rotate( box, 1, 3, 4, 2 );

}

// zÉñ]³¹éÖ
std::vector <int> rotate_z( std::vector <int> box )
{
	// zñ]
	// c1, c2, c3, c4, c5, c6 : default
	// c3, c2, c6, c1, c5, c4

	return rotate( box, 0, 2 ,5 ,3 );

}

// Gg[|Cg
int main()
{

	// Ï
	int n;
	int answer;
	char color[32];
	std::vector <int> box( 6, 0 );
	std::set < std::vector <int> > table;

	while( 1 )
	{
		// ú»
		answer = 0;
		table.clear();
		for( int i = 0; i < 6; ++i )
		{
			box[i] = 0;
		}

		// üÍ
		std::fflush( stdin );
		std::scanf( "%d", &n );

		// üÍÌIíèF[ÐÆÂ
		if( n == 0 ) break;

		// nÌ¾¯ÇÝÞ
		for( int i = 0; i < n; ++i )
		{
			std::fflush( stdin );

			// FîñðüÍ
			for( int j = 0; j < 6; ++j )
			{
				std::scanf( "%s", color );

				// µ¢â·¢æ¤É¶ñ¨®ÉÏ·
				// F: Red, Yellow, Blue, Magenta, Green, Cyan
				if( std::strcmp( "Red", color ) == 0 )
					box[j] = 1;
				else if( std::strcmp( "Yellow", color ) == 0 )
					box[j] = 2;
				else if( std::strcmp( "Blue", color ) == 0 )
					box[j] = 3;
				else if( std::strcmp( "Magenta", color ) == 0 )
					box[j] = 4;
				else if( std::strcmp( "Grdeen", color ) == 0 )
					box[j] = 5;
				else if( std::strcmp( "Cyan", color ) == 0 )
					box[j] = 6;
			}

			// insertÌOÉ¯¶àÌªÈ¢©T·
			// ¯¶àÌª Á½çJEg
			if( table.find( box ) != table.end() )
			{
				++answer;
				continue;
			}

			// insertµÄ¨­

				// x
				for( int j = 0; j < 4; ++j )
				{
					box = rotate_x( box );
					table.insert( box );
					for( int k = 0; k < 6; ++k )
					{
						if( k % 2 == 0 )
						{
							box = rotate_y( box );
							table.insert( box );
						}
						else
						{
							box = rotate_z( box );
							table.insert( box );
						}
					}
				}

		}

		// oÍ
		std::printf( "%d\n", answer );

	}

} // main()