#include <iostream>
#include <vector>
#include <map>
#include <set>
using namespace std;

typedef vector <int> VI;

VI roll_y( VI& box )
{
	VI work = box;
	work[0] = box[2];
	work[2] = box[5];
	work[5] = box[3];
	work[3] = box[0];
	return work;
}

VI roll_x( VI& box )
{
	VI work = box;
	work[5] = box[1];
	work[1] = box[0];
	work[0] = box[4];
	work[4] = box[5];
	return work;
}

VI roll_z( VI& box )
{
	VI work = box;
	work[3] = box[4];
	work[4] = box[2];
	work[2] = box[1];
	work[1] = box[3];
	return work;
}

void register_box( VI& box, set <VI>& V )
{
	V.insert( box );

	VI work;
	
	work = box;
	for ( int i = 0; i < 3; i++ )
	{
		work = roll_y( work );
		V.insert( work );
	}

	work = box;
	for ( int j = 0; j < 3; j++ )
	{
		work = roll_x( work );
		for ( int i = 0; i < 3; i++ )
		{
			work = roll_y( work );
			V.insert( work );
		}
		work = roll_y( work );
	}

	work = box;
	for ( int j = 0; j < 3; j++ )
	{
		work = roll_z( work );
		for ( int i = 0; i < 3; i++ )
		{
			work = roll_y( work );
			V.insert( work );
		}
		work = roll_y( work );
	}

}

int main( void )
{
	// Redツ、Yellowツ、Blueツ、Magentaツ、Greenツ、Cyan
	map <string, int> hash_table;
	hash_table["Red"]		= 1;
	hash_table["Yellow"]	= 2;
	hash_table["Blue"]		= 3;
	hash_table["Magenta"]	= 4;
	hash_table["Green"]		= 5;
	hash_table["Cyan"]		= 6;

	int n;
	while ( cin >> n && n )
	{
		int cnt = 0;
		set < VI > V;
		VI box(6);
		string color;

		for ( int i = 0; i < n; i++ )
		{
			for ( int j = 0; j < 6; j++ )
			{
				cin >> color;
				box[j] = hash_table[color];
			}

			if ( V.find( box ) != V.end() )
				cnt ++;

			register_box( box, V );
		}

		cout << cnt << endl;
	}

	return 0;
}