#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

struct Perm{
    std::vector<int> perm;

    Perm(std::initializer_list<int> p) : perm(p.begin(), p.end()) { }

    static Perm identity(){
        return {0, 1, 2, 3, 4, 5};
    }

    Perm& operator*=(const Perm &rhs){
        std::vector<int> p(6);
        for(int i=0;i<6;++i){
            p[i] = perm[rhs.perm[i]];
        }

        perm = std::move(p);

        return *this;
    }
};

const Perm r1 = {5, 3, 4, 1, 2, 0}, // 表裏をひっくり返す
    r2 = {0, 3, 1, 4, 2, 5}, // 水平面に垂直な軸に関して回転
    r3 = {2, 0, 1, 4, 5, 3}; // 見えている面を変えない回転
std::vector<Perm> perms;

struct Cube{
    std::vector<std::string> surface;

    Cube() = default;
    Cube(std::vector<std::string> v) : surface(std::move(v)) { }

    friend bool operator==(const Cube &lhs, const Cube &rhs){
        for(auto &p : perms){
            bool eq = true;

            for(int i=0;i<6;++i){
                if(lhs.surface[i] != rhs.surface[p.perm[i]]){
                    eq = false;
                    break;
                }
            }

            if(eq){
                return true;
            }
        }

        return false;
    }
};

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::queue<Perm> que;
    que.emplace(Perm::identity());
    que.emplace(r1);
    
    for(int i=0;i<2;++i){
        Perm p = que.front();
        que.pop();

        for(int j=0;j<4;++j){
            que.emplace(p);
            p *= r2;
        }
    }

    for(int i=0;i<8;++i){
        Perm p = que.front();
        que.pop();

        for(int j=0;j<3;++j){
            que.emplace(p);
            p *= r3;
        }
    }

    until(que.empty()){
        perms.emplace_back(que.front());
        que.pop();
    }

    while(true){
        int N;
        std::cin >> N;

        if(N == 0){
            break;
        }

        std::vector<Cube> cubes(N);
    
        for(int i=0;i<N;++i){
            std::vector<std::string> v(6);

            for(int j=0;j<6;++j){
                std::cin >> v[j];
            }

            cubes[i] = std::move(Cube(v));
        }

        ll used = 0;
        int cnt = 0;
        
        for(int i=0;i<N;++i){
            if(used >> i & 1){
                continue;
            }

            cnt += 1;

            for(int j=i+1;j<N;++j){
                if(cubes[j] == cubes[i]){
                    used |= 1ll << j;
                }
            }
        }

        int left = N - cnt;
        std::cout << left << std::endl;
    }
}

