#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#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()
const int INF = 1<<30;

bool eq(string *s1, string *s2) {
  REP(k,4) {
    bool f = true;
    REP(i,4) {
      if (s1[i] != s2[(i+k)%4])
        f = false;
    }
    if (f)
      return true;
  }
  return false;
}

int main() {
  int hoge[6][4] = {{1,3,4,2},{0,2,5,3},{0,4,5,1}
                    ,{0,1,5,4},{0,3,5,2},{1,2,4,3}};
  int n;
  while(cin >> n, n) {
    string side[n][4];
    REP(iii,n) {
      string s[6];
      int p;
      REP(i,6) {
        cin >> s[i];
        if (s[i] == "Red")
          p = i;
      }
      REP(i,4)
        side[iii][i] = s[hoge[p][i]];
    }
    long long kaburi = 0;
    REP(i,n) {
      REP(j,n)
        if (eq(side[i], side[j])) {
          kaburi++;
        }
    }
    // cout << kaburi << endl;
    kaburi -= n;
    // cout << kaburi << endl;
    REP(i,n) {
      if (i*(i+1) == kaburi) {
        cout << i << endl;
        break;
      }
    }
  }
}