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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

const int MAX_N = 30;

class UnionFind{
public:
    UnionFind(){}
    UnionFind(int n){
        REP(i, n){
            par[i] = i;
            rank[i] = 0;
        }
    }
    int find(int x){
        if(x == par[x])return x;
        return par[x] = find(par[x]);
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y){return;}
        
        if(rank[x] > rank[y]){
            par[y] = x;
        }else{
            par[x] = y;
            if(rank[x] == rank[y]){rank[y]++;}
        }
    }
private:
    int rank[MAX_N], par[MAX_N];
};

int N;
int dices[30][6];
std::string colors[6] = {"Red", "Yellow", "Blue", "Magenta", "Green", "Cyan"};
const std::vector<int> route[] = {
    {0, 1, 5, 4},
    {1, 2, 4, 3}
}, remaining[] = {{2, 3}, {0, 5}};

bool same(int i, int j){
    // a: ルート, b: 始点, c: 方向
    REP(a, 2){
        REP(b, 4){
            REP(c, 2){
                REP(d, 2){
                    REP(e, 4){
                        REP(f, 2){
                            int x = b, y = e;
                            bool can = true;
                            REP(g, 4){
                                if(dices[i][route[a][x]] != dices[j][route[d][y]]){can = false; break;}
                                x = (x + dy[c] + 4) % 4; y = (y + dy[f] + 4) % 4;
                            }
                            if(!can){continue;}
                            if((dices[i][remaining[a][0]] == dices[j][remaining[d][0]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][1]]) ||
                               (dices[i][remaining[a][0]] == dices[j][remaining[d][1]] &&
                                dices[i][remaining[a][1]] == dices[j][remaining[d][0]])){
                                return true;
                            }
                        }
                    }
                }
            }
        }
    }
    return false;
}

int main(){
    while(std::cin >> N, N){
        UnionFind uf(N);

        REP(i, N){
            REP(j, 6){
                std::string s;
                std::cin >> s;

                REP(k, 6){
                    if(s == colors[k]){dices[i][j] = k;}
                }
            }
        }

        REP(i, N){
            FOR(j, i+1, N){
                if(same(i, j)){
                    uf.unite(i, j);
                }
            }
        }

        int res = N;
        REP(i, N){
            if(i == uf.find(i)){res -= 1;}
        }

        std::cout << res << std::endl;
    }
}