#include<iostream>
#include<vector>
#include<complex>
#include<algorithm>
#include<set>
#include<map>
#define rep(i,n) for(int i = 0;i < n ; ++i)

using namespace std;

using ll = long long;

struct die{
    int u,d,l,r,b,f;
    die(vector<int> a){
        f=a[0];
        u=a[1];
        l=a[2];
        r=a[3];
        d=a[4];
        b=a[5];

        if(b==0){
            swap(b,f);
            swap(l,r);
        }
        if(d==0){
            int tmp = d;
            d = b; b = u; u = f; f = tmp;
        } 
        if(u==0){
            int tmp = u;
            u = b; b = d; d = f; f = tmp;
        }
        if(l==0){
            int tmp = l;
            l = b; b = r; r = f; f = tmp;
        }
        if(r==0){
            int tmp = r;
            r = b; b = l; l = f; f = tmp;
        }
        int mi = min({u,d,l,r});
        while(u!=mi){
           int tmp = r;
            r = u; u = l; l = d; d = tmp; 
        }
    }
    bool operator==(const die rhs)const{
        return (f==rhs.f&&b==rhs.b&&u==rhs.u&&d==rhs.d&&l==rhs.l&&r==rhs.r);
    }
};

map<string, int> mp;
void solve(int n){
    vector<die> d;
    rep(i,n){
        vector<int> a(6);
        rep(j,6){
            string s;cin>>s;
            a[j]=mp[s];
        }
        die di(a);
        d.push_back(di);
    }
    vector<int> used(n);
    int ans = 0;
    rep(i,n){
        if(used[i]){
            ++ans;
            continue;
        }
        for(int j = i+1;j<n;j++){
            if(d[i]==d[j])used[j]=true;
        }
    }
    cout<<ans<<endl;
}
int main(){
     mp["Cyan"]=0;
     mp["Yellow"]=1;
     mp["Red"]=2;
     mp["Magenta"]=3;
     mp["Green"]=4;
     mp["Blue"]=5;
     int n;
     while(cin>>n,n!=0)solve(n);
}
