#include<iostream>
#include<string>
using namespace std;
struct kyu_bu{
  string kyu[7];
  bool h;
};
kyu_bu p[30];
int main(){
  int n,start,cou,index;
  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++){
      for(int j=1;j<=6;j++) cin >> p[i].kyu[j];
      p[i].h = true;
    }
    for(int i=0;i<n;i++){
      if(!p[i].h) continue;
      for(int j=i+1;j<n;j++){
	if(!p[j].h) continue;
	bool ok = true;;
	for(int g=1;g<=3;g++){
	  if(!ok) break;
	  for(int k=1;k<=6;k++){
	    if(p[i].kyu[g]==p[j].kyu[k]){
	      if(p[i].kyu[6-g+1]!=p[j].kyu[6-k+1]) ok = false;
	    }
	  }
	}
	if(ok) count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}