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

int cc(int,int);

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];
	      for(m=0;m<4;m++){
		c1[m] = cc(k,m);
		c2[m] = cc(l,m);
	      }

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

	      }
	    }
	    if(f) break;
	  }
	  if(f) break;
	}
      }
    }

    cout << ans << endl;

  }
  return 0;
}


int cc(int a,int b){

  if(a == 1){
    if(b == 0) return 5;
    if(b == 1) return 3;
    if(b == 2) return 2;
    if(b == 3) return 4;
  }
  if(a == 2){
    if(b == 0) return 1;
    if(b == 1) return 3;
    if(b == 2) return 6;
    if(b == 3) return 4;
  }
  
  if(a == 3){
    if(b == 0) return 5;
    if(b == 1) return 6;
    if(b == 2) return 2;
    if(b == 3) return 1;
  }
  
  if(a == 4){
    if(b == 0) return 5;
    if(b == 1) return 1;
    if(b == 2) return 2;
    if(b == 3) return 6;
  }
  
  if(a == 5){
    if(b == 0) return 6;
    if(b == 1) return 3;
    if(b == 2) return 1;
    if(b == 3) return 4;
  }
  
  if(a == 6){
    if(b == 0) return 5;
    if(b == 1) return 4;
    if(b == 2) return 2;
    if(b == 3) return 3;
  }
}