#include<iostream>
#include<string>
using namespace std;
struct kyu_bu{
  string kyu[7];
  bool h;
};
kyu_bu p[30];
void North(int i){
  string mem;
  mem = p[i].kyu[1];
  p[i].kyu[1] = p[i].kyu[2];
  p[i].kyu[2] = p[i].kyu[6];
  p[i].kyu[6] = p[i].kyu[5];
  p[i].kyu[5] = mem;
}
void East(int i){
  string mem;
  mem = p[i].kyu[1];
  p[i].kyu[1] = p[i].kyu[4];
  p[i].kyu[4] = p[i].kyu[6];
  p[i].kyu[6] = p[i].kyu[3];
  p[i].kyu[3] = mem;
}
void Right(int i){
  string mem;
  mem = p[i].kyu[2];
  p[i].kyu[2] = p[i].kyu[3];
  p[i].kyu[3] = p[i].kyu[5];
  p[i].kyu[5] = p[i].kyu[4];
  p[i].kyu[4] = mem;
}
int main(){
  int n;
  int roop[7]={0,2,3,4,5,6,1};
  while(cin >> n){
    if(n==0) break;
    int count = 0;
    for(int i=0;i<n;i++){
      p[i].h = true;
      for(int j=1;j<=6;j++) cin >> p[i].kyu[j];
    }
    for(int i=0;i<n-1;i++){
      if(!p[i].h) continue;
      for(int j=0;j<=12;j++){
	if(j>=1&&j<=4) North(i);
	else if(j>=5&&j<=8) East(i);
	else if(j>=9&&j<=12)Right(i);
	for(int k=i+1;k<n;k++){
	  if(!p[k].h) continue;
	  for(int g=1;g<=6;g++){
	    if(p[i].kyu[1]==p[k].kyu[g]){
	      int strat = roop[g];
	      int cou = 1;
	      int index = 2;
	      while(cou!=6){
		if(p[i].kyu[index]!=p[k].kyu[strat]) break;
		cou++;
		index++;
		strat = roop[strat];   
	      }
	      if(cou==6){
		count++;
		p[k].h = false;
	      }
	    }
	  }
	}
      }
    }
    cout << count << endl;
  }
  return 0;
}
	