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

int main(){
  int i,j,k,l,m,o,n,a[30][7],ans,c;
  string str;
  bool f;
  while(1){
    cin >> n;
    if(n == 0) break;

    for(int t=0;t<n;t++){
      for(i=1;i<7;i++){
	cin >> str;
	if(str == "Cyan") a[t][i] = 1;
	else if(str == "Yellow") a[t][i] = 2;
	else if(str == "Red") a[t][i] = 3;
	else if(str == "Magenta") a[t][i] = 4;
	else if(str == "Green") a[t][i] = 5;
	else a[t][i] = 6;
      }
    }
    ans = 0;

    for(i=0;i<n-1;i++){
      for(j=i+1;j<n;j++){
	f = false;
	for(k=1;k<7;k++){
	  for(l=1;l<7;l++){
	    if(a[i][k] == a[j][l] /*&& a[i][7-k] == a[j][7-l]*/){
	      f = true;
	      int c1[4],c2[4];
	      if(k == 1){
		c1[0] = 5;
		c1[1] = 3;
		c1[2] = 2;
		c1[3] = 4;
	      }
	      if(l == 1){
		c2[0] = 5;
		c2[1] = 3;
		c2[2] = 2;
		c2[3] = 4;
	      }
	      if(k == 2){
		c1[0] = 1;
		c1[1] = 3;
		c1[2] = 5;
		c1[3] = 4;
	      }
	      if(l == 2){
		c2[0] = 1;
		c2[1] = 3;
		c2[2] = 5;
		c2[3] = 4;
	      }
	      if(k == 3){
		c1[0] = 5;
		c1[1] = 6;
		c1[2] = 2;
		c1[3] = 1;
	      }
	      if(l == 3){
		c2[0] = 5;
		c2[1] = 6;
		c2[2] = 2;
		c2[3] = 1;
	      }
	      if(k == 4){
		c1[0] = 5;
		c1[1] = 1;
		c1[2] = 2;
		c1[3] = 6;
	      }
	      if(l == 4){
		c2[0] = 5;
		c2[1] = 1;
		c2[2] = 2;
		c2[3] = 6;
	      }
	      if(k == 5){
		c1[0] = 6;
		c1[1] = 3;
		c1[2] = 1;
		c1[3] = 4;
	      }
	      if(l == 5){
		c2[0] = 6;
		c2[1] = 3;
		c2[2] = 1;
		c2[3] = 4;
	      }
	      if(k == 6){
		c1[0] = 5;
		c1[1] = 4;
		c1[2] = 2;
		c1[3] = 3;
	      }
	      if(l == 6){
		c2[0] = 5;
		c2[1] = 4;
		c2[2] = 2;
		c2[3] = 3;
	      }

	      for(m=0;m<4;m++){
		c = 0;
		for(o=0;o<4;o++){
		  if(a[i][c1[o]] == a[i][c2[(o+m)%4]]) c++;
		}
		if(c == 4){
		  ans++;
		  break;
		}
	      }
	    }
	    if(f) break;
	  }
	  if(f) break;
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}