#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>

using namespace std;
typedef long long LL;
static const double EPS = 1e-9;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n) 

int main(void){  
  int n;
  vector<string> col;
  col.push_back("Red");
  col.push_back("Yellow");
  col.push_back("Blue");
  col.push_back("Magenta");
  col.push_back("Green");
  col.push_back("Cyan");
  while(cin>>n,n){
    int c[30][6] = {0};
    int check[30] = {0};
    int t[30][6];
    REP(i,n)
      REP(j,6){
        string tmp;
        cin >> tmp;
        REP(k,6){
          if(col[k] == tmp){
            c[i][j] = k;
            break;
          }
        }
      }
    int count = 0;
    REP(i,n)
      REP(j,6)
        t[i][c[i][j]] = c[i][5-j];
    REP(i,n){
      if(check[i] == 0){
        int check1=0;
        REP(l,3){
          if(c[i][l]<t[i][c[i][l]]) check1++;
        }
        FOR(j,i+1,n){
          int check2 = 0;
          int check3 = 0;
          bool f = true;
          REP(k,6){
            if(t[i][k] != t[j][k]) f=false;
          }
          REP(l,3){
            if(c[j][l]<t[j][c[j][l]]) check2++;
          }
          REP(l,3){
            if(c[i][l] != c[j][l] && c[i][l] != t[j][c[j][l]]) check3++;
          }
          if(check3>0) check3--;
          if((check1+check2+check3)%2!=0) f=false;
          if(f){
            check[j] = 1;
            count++;
          }
        }
      }
    }
    cout<<count<<endl;
  }
}