#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
class CUBE{
private:
	string men[6];
public:
	void spinl(){string temp;temp=men[0];men[0]=men[2];men[2]=men[5];men[5]=men[3];men[3]=temp;}
	void spinu(){string temp;temp=men[0];men[0]=men[1];men[1]=men[5];men[5]=men[4];men[4]=temp;}
	string get(int element){return men[element]; }
	void set(){cin>>men[0]>>men[1]>>men[2]>>men[3]>>men[4]>>men[5];}
};
CUBE *cube;
int main(){
	int n;
	int *flg;
	int counter=0;
	scanf("%d",&n);
	while(n!=0){
		//初期化
		counter=0;
		cube=new CUBE [n];
		//入力部
		flg=new int [n];
		for(int i=0;i<n;i++){
			cube[i].set();
			flg[i]=0;
		}
		//処理部
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				if(cube[i].get(0)==cube[j].get(0)&&cube[i].get(1)==cube[j].get(1)&&cube[i].get(2)==cube[j].get(2)&&cube[i].get(3)==cube[j].get(3)&&cube[i].get(4)==cube[j].get(4)&&cube[i].get(5)==cube[j].get(5)){
					flg[j]=1;
				}
				else{
					for(int n1=0;n1<4;n1++){
						cube[j].spinl();
						for(int n2=0;n2<4;n2++){
							cube[j].spinu();
							if(cube[i].get(0)==cube[j].get(0)&&cube[i].get(1)==cube[j].get(1)&&cube[i].get(2)==cube[j].get(2)&&cube[i].get(3)==cube[j].get(3)&&cube[i].get(4)==cube[j].get(4)&&cube[i].get(5)==cube[j].get(5)){
								flg[j]=1;
							}
						}
					}
				}
			}
		}
		for(int i=0;i<n;i++){
			counter+=flg[i];
		}
		printf("%d\n",counter);
		delete []cube;
		scanf("%d",&n);
	}
	return 0;
}