
// 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>

#define FOREACH( TYPE, TARGET, IT ) \
	for( (TYPE)::iterator IT = (TARGET).begin(); IT != (TARGET).end(); ++(IT) )

// Ö
std::vector <int> rotate_h( std::vector <int> box );
std::vector <int> rotate_v( std::vector <int> box );

// ¡Éñ]³¹éÖ
std::vector <int> rotate_h( std::vector <int> box )
{
	// ¡ñ]
	// c1, c2, c3, c4, c5, c6 : default
	// c3, c2, c6, c1, c5, c4
	
	int tmp	= box[0];
	box[0]	= box[2];
	box[2]	= box[5];
	box[5]	= box[3];
	box[3]	= tmp;

	return box;

}

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

	int tmp = box[0];
	box[0] = box[4];
	box[4] = box[5];
	box[5] = box[1];
	box[1] = tmp;

	return box;

}

// Gg[|Cg
int main()
{
	// §ûÌÌ6ÊðyLÅFt¯µ½àÌðìiÆµÄoWµ½
	// §ûÌðñ]³¹éÆ¯¶gÝí¹ÉÈÁÄµÜ¤±ÆÉCªt¢½
	// d¡ð³­µ½Æ«ÇÁÅ½Âìè¼·Kvª é©ßé



	// Ï
	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( "Green", 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µÄ¨­
			// ¡¨cx3ð4ñ
			for( int j = 0; j < 4; ++j )
			{
				if( j > 0 )
				{
					box = rotate_h( box );
				}
				table.insert( box );
				
				// c
				for( int k = 0; k < 4; ++k )
				{
					box = rotate_v( box );
					table.insert( box );
				}
			}
		}

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

	}

} // main()