#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
string s[50][6];
int d[12][6]={
  {0,1,2,3,4,5},
  {0,2,3,4,1,5},
  {0,3,4,1,2,5},
  {0,4,1,2,3,5},
  
  {0,1,2,3,4,5},
  {2,1,3,4,0,5},
  {3,1,4,0,2,5},
  {4,1,0,2,3,5},
  
  {0,1,2,3,4,5},
  {1,4,2,3,5,0},
  {4,5,2,3,0,1},
  {5,0,2,3,1,4}
};
int main(){
  int n;
  while(cin>>n&&n){
    int ans=0;
    lol(i,n){
      lol(j,6)cin>>s[i][j];
      bool same=false;
      lol(j,i){
	lol(r,12){
	  bool ok=true;
	  lol(k,6){
	    if(s[i][k]!=s[j][d[r][k]])ok=false;
	  }
	  if(ok)same=true;
	}
      }
      if(same)ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}