#include<iostream>
#include<string>
#include<vector>

using namespace std;

struct DATA{
  string s1, s2, s3, s4, s5, s6;
};

void Swap(string&, string&, string&, string&);
DATA MakeData(string, string, string, string, string, string);
int SearchTheSame(DATA, DATA);

int main(){
  int i, j, n, x;
  string s1, s2, s3, s4, s5, s6;
  DATA _data;
  vector<DATA> data;

  while(1){
    cin >> n;
    if(n == 0) break;

    for(i=0; i<n; ++i){
      cin >> s1 >> s2 >> s3 >> s4 >> s5 >> s6;

      if(s2 == "Red"){
	Swap(s1, s2, s6, s5);
      }else if(s3 == "Red"){
	Swap(s1, s3, s6, s4);
      }else if(s4 == "Red"){
	Swap(s1, s4, s6, s3);
      }else if(s5 == "Red"){
	Swap(s1, s5, s6, s2);
      }else if(s6 == "Red"){
	Swap(s1, s2, s6, s5);
	Swap(s1, s2, s6, s5);
      }

      _data = MakeData(s1, s2, s3, s4, s5, s6);

      for(j=0, x=0; j<data.size(); ++j){
	if(SearchTheSame(data[j], _data) == 1){
	  x = 1;
	  break;
	}
      }
      if(x == 0) data.push_back(_data);
    }

    cout << n-data.size() << endl;
    data.clear();
  }
  return 0;
}

void Swap(string& a, string& b, string& c, string& d){
  string e;
  e = a;
  a = b;
  b = c;
  c = d;
  d = e;
}

DATA MakeData(string s1, string s2, string s3, string s4, string s5, string s6){
  DATA data;
  data.s1 = s1;
  data.s2 = s2;
  data.s3 = s3;
  data.s4 = s4;
  data.s5 = s5;
  data.s6 = s6;
  return data;
}

int SearchTheSame(DATA data1, DATA data2){
  if(data1.s1 == data2.s1 && data1.s2 == data2.s2 && data1.s3 == data2.s3 && 
     data1.s4 == data2.s4 && data1.s5 == data2.s5 && data1.s6 == data2.s6)
    return 1;

  Swap(data2.s2, data2.s3, data2.s5, data2.s4);

  if(data1.s1 == data2.s1 && data1.s2 == data2.s2 && data1.s3 == data2.s3 && 
     data1.s4 == data2.s4 && data1.s5 == data2.s5 && data1.s6 == data2.s6)
    return 1;

  Swap(data2.s2, data2.s3, data2.s5, data2.s4);

  if(data1.s1 == data2.s1 && data1.s2 == data2.s2 && data1.s3 == data2.s3 && 
     data1.s4 == data2.s4 && data1.s5 == data2.s5 && data1.s6 == data2.s6)
    return 1;

  Swap(data2.s2, data2.s3, data2.s5, data2.s4);

  if(data1.s1 == data2.s1 && data1.s2 == data2.s2 && data1.s3 == data2.s3 && 
     data1.s4 == data2.s4 && data1.s5 == data2.s5 && data1.s6 == data2.s6)
    return 1;

  return 0;
}