#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    while(true){
        cin>>n;
        if(n==0)break;
        vector<vector<int>> d;
        int ans=0;
        map<string,int> mp;
        mp["Red"]=0;
        mp["Yellow"]=1;
        mp["Blue"]=2;
        mp["Magenta"]=3;
        mp["Green"]=4;
        mp["Cyan"]=5;
        const int sa[6][4]={
                {4,2,1,3},
                {0,2,5,3},
                {4,5,1,0},
                {4,0,1,5},
                {5,2,0,3},
                {4,3,1,2}
        };
        for (int i = 0; i < n; ++i) {
            vector<string> s(6);
            int id;
            for (int j = 0; j < 6; ++j) {
                cin>>s[j];
                if(s[j]=="Red")id=j;
            }
            vector<int> tv;
            for (int j = 0; j < 4; ++j) {
                tv.emplace_back(mp[s[sa[id][j]]]);
            }
            for(auto item:d){
                for (int j = 0; j < 4; ++j) {
                    bool f=true;
                    for (int k = 0; k < 4; ++k) {
                        if(tv[k]!=item[(j+k)%4]){
                            f=false;
                            break;
                        }
                    }
                    if(f){
                        ++ans;
                        goto failed;
                    }
                }
            }
            d.emplace_back(tv);
            failed:;
        }
        cout<<ans<<endl;
    }
    return 0;
}
