
#include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define TOP 0 
#define BOTTOM 1
#define FRONT 2
#define BACK 3
#define RIGHT 4
#define LEFT 5

vector< vector<string> > check;

void z(vector<string> &dice){
	string to[6];
	to[BACK] = dice[TOP];
	to[BOTTOM] = dice[BACK];
	to[FRONT] = dice[BOTTOM];
	to[TOP] = dice[FRONT];

	to[LEFT] = dice[LEFT];
	to[RIGHT] = dice[RIGHT];

	rep(i,6)dice[i] = to[i];

}

void x(vector<string> &dice){
	string to[6];
	to[FRONT] = dice[LEFT];
	to[LEFT] = dice[BACK];
	to[BACK] = dice[RIGHT];
	to[RIGHT] = dice[FRONT];
	
	to[BOTTOM] = dice[BOTTOM];
	to[TOP] = dice[TOP];
	
	rep(i,6)dice[i] = to[i];
}

void y(vector<string> &dice){
	string to[6];
	to[TOP] = dice[LEFT];
	to[LEFT] = dice[BOTTOM];
	to[BOTTOM] = dice[RIGHT];
	to[RIGHT] = dice[TOP];
	
	to[FRONT] = dice[FRONT];
	to[BACK] = dice[BACK];
	
	rep(i,6)dice[i] = to[i];
}

bool isunique(vector<string> data){

	rep(l1,4)rep(l2,4)rep(l3,4){
	
		vector<string> t(6);
		
		rep(j,6)t[j] = data[j];

		rep(j,l1)x(t);
		rep(j,l2)y(t);
		rep(j,l3)z(t);
		
		rep(i,check.size()){
			bool flag = true;
			rep(j,6)if(t[j] != check[i][j])flag = false;
			if(flag)return false;
		}

	}
	return true;
}

int main(){
	int n;
	string c[6];
	while(cin >> n, n){
		check.clear();
		rep(i,n){
			rep(j,6)cin >> c[j];
			string d[6] =  {c[0],c[5],c[1],c[4],c[2],c[3]};
			vector<string> dice(6);
			rep(j,6)dice[j] = d[j];
			
			if( isunique(dice) ){
				check.push_back(dice);
			}
		}
		cout << n-check.size() << endl;
	}
}