#include<bits/stdc++.h>
using namespace std;

//??????????????§??\??????????????¨??????
const int TOP=0,BOTTOM=1,FRONT=2,BACK=3,LEFT=4,RIGHT=5;

template<class T>
class dice{
public:
    dice(){
        for(int i=0;i<6;i++)id[i]=i;
    }

    T& operator[](int f){return var[id[f]];}
    const T& operator[](int f)const{return var[id[f]];}

    //???????????????????????´????????????
    bool operator==(const dice<T>&b)const{
        const dice<T>&a=*this;
        for(int i=0;i<6;i++)if(a[i]!=b[i])return false;
        return true;
    }

    //????¨??????¢??????
    void roll_x(){roll(TOP,BACK,BOTTOM,FRONT);}
    void roll_y(){roll(TOP,LEFT,BOTTOM,RIGHT);}
    void roll_z(){roll(FRONT,RIGHT,BACK,LEFT);}

    //?????¢??????????????\???24???????????¶????????????
    vector<dice<T> >all_rolls(){
        vector<dice<T> >ret;
        for(int k=0;k<6;(k&1?roll_y():roll_x()),k++){
            for(int i=0;i<4;roll_z(),i++){
                ret.push_back(*this);
            }
        }
        return ret;
    }

    bool equivalent_to(const dice<T>& di){
        vector<dice<T> >vec=all_rolls();
        for(int i=0;i<vec.size();i++){
            if(vec[i]==di)return true;
        }
        return false;
    }
private:
    void roll(int a,int b,int c,int d){
        int tmp=id[a];
        id[a]=id[b];id[b]=id[c];
        id[c]=id[d];id[d]=tmp;
    }

    T var[6];
    int id[6];
};

int main(){
    int N;
    while(cin>>N,N){
        vector<dice<string> >vec;

        int cnt=0;
        while(N--){
            dice<string>d;
            cin>>d[TOP];
            cin>>d[FRONT];
            cin>>d[RIGHT];
            cin>>d[LEFT];
            cin>>d[BACK];
            cin>>d[BOTTOM];

            bool ok=true;
            for(int i=0;i<vec.size();i++)
                if(vec[i].equivalent_to(d))ok=false;
            if(ok)vec.push_back(d);
            else cnt++;
        }

        cout<<cnt<<endl;
    }

    return 0;
}