#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

struct cube{
  int c[6];
  inline int &operator [] (int i){
    return c[i-1];
  }
};

void rotate2(cube &c){
  int tmp = c[1];
  c[1] = c[5];
  c[5] = c[6];
  c[6] = c[2];
  c[2] = tmp;
}
void rotate3(cube &c){
  int tmp = c[1];
  c[1] = c[4];
  c[4] = c[6];
  c[6] = c[3];
  c[3] = tmp;
}
void rotate4(cube &c){
  int tmp = c[1];
  c[1] = c[3];
  c[3] = c[6];
  c[6] = c[4];
  c[4] = tmp;
}
void rotate5(cube &c){
  int tmp = c[1];
  c[1] = c[2];
  c[2] = c[6];
  c[6] = c[5];
  c[5] = tmp;
}
void rotate23(cube &c){
  int tmp = c[2];
  c[2] = c[4];
  c[4] = c[5];
  c[5] = c[3];
  c[3] = tmp;
}
void rotate24(cube &c){
  int tmp = c[2];
  c[2] = c[3];
  c[3] = c[5];
  c[5] = c[4];
  c[4] = tmp;
}

inline bool operator < (const cube &a, const cube &b){
  REP(i,6){
    if(a.c[i] != b.c[i]) return a.c[i] < b.c[i];
  }
  return false;
}

int main(){
  map<string,int> m;
  m["Red"]=0; m["Yellow"]=1; m["Blue"]=2;
  m["Magenta"]=3; m["Green"]=4; m["Cyan"]=5;
  int n;
  while(cin>>n, n){
    set<cube> s;
    REP(i,n){
      cube c;
      REP(j,6){
	string tmp; cin>>tmp;
	c[j+1] = m[tmp];
      }
      if(c[2] == 0){
	rotate5(c);
      }else if(c[3] == 0){
	rotate4(c);
      }else if(c[4] == 0){
	rotate3(c);
      }else if(c[5] == 0){
	rotate2(c);
      }else if(c[6] == 0){
	rotate5(c);
	rotate5(c);
      }

      if(c[6] != 1){
	if(c[3] == 1){
	  rotate24(c);
	}else if(c[4] == 1){
	  rotate23(c);
	}else if(c[5] == 1){
	  rotate24(c);
	  rotate24(c);
	}
      }else{
	if(c[3] == 2){
	  rotate24(c);
	}else if(c[4] == 2){
	  rotate23(c);
	}else if(c[5] == 2){
	  rotate24(c);
	  rotate24(c);
	}
      }
      s.insert(c);
    }
    printf("%d\n",n-s.size());
  }
  return 0;
}