#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cmath>
#include <queue>

using namespace std;

bool check[6][6][6][6][6][6];

int index(string str){
	if(str == "Cyan"){
		return 0;
	}else if(str == "Yellow"){
		return 1;
	}else if(str == "Red"){
		return 2;
	}else if(str == "Magenta"){
		return 3;
	}else if(str == "Green"){
		return 4;
	}else{
		return 5;
	}
}

void rotate_1(int col[6]){
	int dummy = col[1];
	col[1] = col[2];
	col[2] = col[4];
	col[4] = col[3];
	col[3] = dummy;
}

void rotate_2(int col[6]){
	int dummy = col[3];
	col[3] = col[0];
	col[0] = col[2];
	col[2] = col[5];
	col[5] = dummy;
}

void rotate_3(int col[6]){
	int dummy = col[0];
	col[0] = col[1];
	col[1] = col[5];
	col[5] = col[4];
	col[4] = dummy;
}

void fill_rotate(int col[6]){
	for(int i = 0; i < 4; i++){
		for(int j = 0; j < 4; j++){
			rotate_2(col);
			check[col[0]][col[1]][col[2]][col[3]][col[4]][col[5]] = true;
			for(int piyo = 0; piyo < 6; piyo++){
				//cout << col[piyo] << " ";
			}
			//cout << endl;
		}
		rotate_1(col);
	}
	rotate_3(col);
	for(int i = 0; i < 4; i++){
		rotate_2(col);
		check[col[0]][col[1]][col[2]][col[3]][col[4]][col[5]] = true;
			for(int piyo = 0; piyo < 6; piyo++){
				//cout << col[piyo] << " ";
			}
			//cout << endl;
	}
	rotate_3(col);
	rotate_3(col);
	for(int i = 0; i < 4; i++){
		rotate_2(col);
		check[col[0]][col[1]][col[2]][col[3]][col[4]][col[5]] = true;
			for(int piyo = 0; piyo < 6; piyo++){
				//cout << col[piyo] << " ";
			}
			//cout << endl;
	}
}

int main(){
	
	int n;
	
	while(true){
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		int col[6];
		
		for(int i = 0; i < 6; i++){
		for(int j = 0; j < 6; j++){
		for(int k = 0; k < 6; k++){
		for(int l = 0; l < 6; l++){
		for(int m = 0; m < 6; m++){
		for(int n = 0; n < 6; n++){
			check[i][j][k][l][m][n] = false;
		}}}}}}
		
		int ans = 0;
		
		for(int loop = 0; loop < n; loop++){
			for(int i = 0; i < 6; i++){
				string str;
				cin >> str;
				col[i] = index(str);
			}
			
			if(!check[col[0]][col[1]][col[2]][col[3]][col[4]][col[5]]){
				ans++;
				fill_rotate(col);
			}
		}
		
		cout << n - ans << endl;
		
	}
	
	return 0;
}