#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

bool arrequ(int a1[], int a2[], int size){
	for(int i = 0;i < size;i++){
		bool equ = true;
		for(int j = 0;j < size;j++){
			if(a1[j] != a2[(i + j) % size]){
				equ = false;
			}
		}
		if(equ) return true;
	}
	return false;
}

void arrrev(int arr[], int size){
	for(int i = 0;i < size / 2;i++){
		swap(arr[i], arr[size - i - 1]);
	}
}

int main_(){
	int a1[4], a2[4];
	for(int i = 0;i < 4;i++) cin >> a1[i];
	for(int i = 0;i < 4;i++) cin >> a2[i];
	cout << arrequ(a1, a2, 4) << endl;
	return 0;
}

int main(){
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		int box[30][6] = {0};
		for(int i = 0;i < n;i++){
			for(int j = 0;j < 6;j++){
				string c;
				cin >> c;
				if(c == "Red") box[i][j] = 0;
				else if(c == "Yellow") box[i][j] = 1;
				else if(c == "Blue") box[i][j] = 2;
				else if(c == "Magenta") box[i][j] = 3;
				else if(c == "Green") box[i][j] = 4;
				else if(c == "Cyan") box[i][j] = 5;
			}
		}
		int set1[3][4] = {
			{1, 2, 4, 3}, 
			{0, 2, 5, 3},
			{0, 1 ,5, 4}
		};
		int set2[3][2] = {
			{0, 5},
			{1, 4},
			{2, 3}
		};
		int ans = 0;
		for(int i1 = 0;i1 < n;i1++){
			for(int i2 = i1 + 1;i2 < n;i2++){
				int a1[4] = {0}, a2[4] = {0}, b1[2] = {0}, b2[2] = {0};
				for(int j = 0;j < 3;j++){
					for(int k = 0;k < 4;k++){
						a1[k] = box[i1][set1[j][k]];
						a2[k] = box[i2][set1[j][k]];
					}
					for(int k = 0;k < 2;k++){
						b1[k] = box[i1][set2[j][k]];
						b2[k] = box[i2][set2[j][k]];
					}
					if(arrequ(a1, a2, 4) && arrequ(b1, b2, 2)){
						//cout << i1 << " & " << i2 << endl;
						ans++;
						break;
					}
					arrrev(a1, 4);
					arrrev(b1, 2);
					if(arrequ(a1, a2, 4) && arrequ(b1, b2, 2)){
						//cout << i1 << " & " << i2 << endl;
						ans++;
						break;
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}