#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1001001001;

struct Dice
{
	int data[6];
	void roll(int a, int b, int c, int d)
	{
		int tmp = data[d];
		data[d] = data[c];
		data[c] = data[b];
		data[b] = data[a];
		data[a] = tmp;
	}
	void rotateX(){
		roll(0, 4, 5, 1);
	}
	void rotateY(){
		roll(0, 2, 5, 3);
	}
	void rotateZ(){
		roll(1, 2, 4, 3);
	}
	int hash()
	{
		int ret = INF;
		rep(i, 6){
			rep(j, 4){
				int h = 0;
				rep(k, 6)
					h = h*6 + data[k];
				ret = min(ret, h);
				rotateX();
			}
			i%2 ? rotateY() : rotateZ();
		}
		return ret;
	}
};

int main()
{
	const char* colors[] = {"Cyan", "Yellow", "Red", "Magenta", "Green", "Blue"};
	map<string, int> dict;
	rep(i, 6)
		dict[colors[i]] = i;
	
	int n;
	while(scanf("%d", &n), n){
		int hash[30];
		rep(i, n){
			Dice dice;
			rep(j, 6){
				char str[128];
				scanf("%s", str);
				dice.data[j] = dict[str];
			}
			hash[i] = dice.hash();
		}
		sort(hash, hash+n);
		printf("%d\n", n - (unique(hash, hash+n)-hash));
	}
	return 0;
}