#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <set>
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;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

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]];
//        cout << side[iii][i] << " ";
      }
      //    cout << endl;
    }
    UnionFind u(n);
    REP(i,n-1) {
      for (int j=i+1; j<n; ++j) {
        if (eq(side[i], side[j])) {
          u.unionSet(i,j);
        }
      }
    }
    set<int> se;
    REP(i,n) {
      se.insert(u.root(i));
    }
    cout << n-se.size() << endl;
  }
}