#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
struct UnionFind{
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x,int y){
        x=root(x);y=root(y);
        if(x==y)return 0;
        if(data[y]<data[x])swap(x,y);
        data[x]+=data[y],data[y]=x;
        return 1;
    }
    bool findSet(int x,int y){
        return root(x)==root(y);
    }
    int root(int x){
        return data[x]<0 ? x : data[x]=root(data[x]);
    }
    int size(int x){
        return -data[root(x)];
    }
};
int main(){
	int n;
	while(cin>>n,n){
		vector<vector<string> >in(n,vector<string>(6));
		rep(i,n)rep(j,6)cin>>in[i][j];
		int a[]={3,1,0,5,4,2};
		rep(i,n){
			if(in[i][1]!="Blue"&&in[i][4]!="Blue")
			while(in[i][0]!="Blue"){
				vector<string>tmp(6);
				rep(j,6)tmp[j]=in[i][a[j]];
				in[i]=tmp;
			}
			
		}
		int b[]={4,0,2,3,5,1};
		rep(i,n){
			while(in[i][0]!="Blue"){
				vector<string>tmp(6);
				rep(j,6)tmp[j]=in[i][b[j]];				
				in[i]=tmp;
			}
		}
		UnionFind uf(n);
		int co=0;
		rep(i,n)rep(j,n)if(i!=j&&in[i][5]==in[j][5]){
			int c[]={0,3,1,4,2,5};
			while(in[i][1]!=in[j][1]){
				vector<string>tmp(6);
				rep(k,6)tmp[k]=in[j][c[k]];
				in[j]=tmp;
			}
			bool h=true;
			rep(k,6)if(in[i][k]!=in[j][k])h=false;
			if(h){
				if(!uf.findSet(i,j))co++;
				uf.unionSet(i,j);
			}
		}
		cout<<co<<endl;
	}
}