#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int colornum(string color){
  //cout<<color<<endl;
  if(color=="Red")return 0;
  if(color=="Yellow")return 1;
  if(color=="Blue")return 2;
  if(color=="Magenta")return 3;
  if(color=="Green")return 4;
  if(color=="Cyan")return 5;
  return 0;
}

int main(){
  int n;
  while(true){
    cin>>n;
    if(!n)break;
    set<int> dice;
    for(int i=0;i<n;i++){
      int d=0;
      int red = 0;
      vector<string> colors(6);
      for(int j=0;j<6;j++){
	cin>>colors[j];
	if (colors[j]=="Red")
	  red = j;
      }
      d = colornum(colors[5-red])*1296;
      int side[4];
      if(red==0)side[0] = 1,side[1]=2;
      if(red==1)side[0] = 0,side[1]=3;
      if(red==2)side[0] = 0,side[1]=1;
      if(red==3)side[0] = 0,side[1]=4;
      if(red==4)side[0] = 0,side[1]=2;
      if(red==5)side[0] = 1,side[1]=3;
      side[2] = 5-side[0];
      side[3] = 5-side[1];
      int dmin = 1296;
      for(int j=0;j<4;j++){
	int dd = 0;
	for(int k=0;k<4;k++){
	  dd *= 6;
	  dd += colornum(colors[side[(j+k)%4]]);
	}
	if(dd<dmin)dmin=dd;
      }
      d += dmin;
      cout<<d<<endl;
      dice.insert(d);
    }
    cout<<(n-dice.size())<<endl;
  }
  return 0;
}