#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

int main()
{
	char* color = "RYBMGC";
	int n;
	while (scanf("%d", &n), n)
	{
		int ans = 0;

		set<int> used;
		while (n--)
		{
			int c[6];
			char buf[16];
			int fixed;
			for (int i = 0; i < 6; ++i)
			{
				scanf(" %s", buf);
				int j;
				for (j = 0; color[j] != *buf; ++j)
					;
				c[i] = j;
				if (j == 0)
					fixed = i;
			}
			int r[][4] = { 
				{ 1, 2, 4, 3 },
				{ 5, 2, 0, 3 },
				{ 0, 1, 5, 4 },
				{ 0, 4, 5, 1 },
				{ 0, 2, 5, 3 },
				{ 2, 1, 3, 4 }
			};
			int rota[4];
			for (int i = 0; i < 4; ++i)
				rota[i] = c[r[fixed][i]];

			bool u = false;
			for (int i = 0; i < 3; ++i)
			{
				int s = 0;
				for (int j = 0; j < 4; ++j)
					s = s*10 + rota[(i+j) % 4];
				if (used.count(s))
				{
					++ans;
					break;
				}
				else
					used.insert(s);
			}
		}
		printf("%d\n", ans);
	}

	return 0;
}