#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

#define rotate_swap(x,a,b,c,d) swap(x.a,x.b);swap(x.b,x.c);swap(x.c,x.d);
 
class Dice{
public:
  int top,front,right,left,back,bottom;
  bool operator<(const Dice &a)const{
    if (top != a.top)return top < a.top;
    if (front != a.front)return front < a.front;
    if (right != a.right)return right < a.right;
    if (left != a.left)return left < a.left;
    if (back != a.back)return back < a.back;
    if (bottom != a.bottom)return bottom < a.bottom;
    return false;
  }
};
 
void rotate_r(Dice &x){
 rotate_swap(x,top,left,bottom,right);
}
 
void rotate_l(Dice &x){
 rotate_swap(x,top,right,bottom,left);
}
 
void rotate_f(Dice &x){
 rotate_swap(x,top,back,bottom,front);
}
 
void rotate_b(Dice &x){
 rotate_swap(x,top,front,bottom,back);
}
 
void rotate_cw(Dice &x){
  rotate_swap(x,back,left,front,right);
}
 
void rotate_ccw(Dice &x){
  rotate_swap(x,back,right,front,left);
}
 
Dice data[24];

void generate_all(Dice &x){
  rep(i,6){
   rep(j,4){
     data[i*4+j]=x;
     rotate_cw(x);
   }
   if (i%2 == 0)rotate_r(x);
   else rotate_f(x);
 }
}

main(){
  string hoge[6]={"Red","Yellow","Blue","Magenta","Green","Cyan"};
  int n;
  while(cin>>n && n){
    set<Dice> S;
    rep(i,n){
      int a[6];
      rep(j,6){
	string tmp;
	cin>>tmp;
	rep(k,6)if (hoge[k] == tmp)a[j]=k;
      }
      Dice d={a[0],a[1],a[2],a[3],a[4],a[5]};
      generate_all(d);
      bool isok=true;
      rep(j,24){
	if (S.find(data[j]) != S.end()){
	  isok=false;
	}
      }
      if (isok)S.insert(d);
    }
    cout << n-S.size() << endl;
  }
  return false;
}