#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

vector<string> rotate(vector<string> src,int t[]){
	vector<string> tmp(6);
	for(int i = 0 ; i < 6 ; i++){
		tmp[t[i]] = src[i];
	}
	return tmp;
}


vector<string> le(vector<string> src){
	//int s[] = {0,1,2,3,4,5};
	int t[] = {0,2,4,1,3,5};
	return rotate(src,t);
}
vector<string> no(vector<string> src){
	//int s[] = {0,1,2,3,4,5};
	int t[] = {1,5,2,3,0,4};
	return rotate(src,t);
}

vector<string> we(vector<string> src){
	//int s[] = {0,1,2,3,4,5};
	int t[] = {3,1,0,5,4,2};
	return rotate(src,t);
}
int main(){
	int n;
	while(cin >> n && n){
		set< vector<string> > c;
		int k = 0;
		for(int i = 0 ; i < n ; i++){
			vector<string> s;
			for(int j = 0 ; j < 6 ; j++){
				string t;cin >> t;
				s.push_back(t);
			}
			if( c.count(s) ) k++;
			for(int x = 0 ; x < 4 ; x++){
				for(int y = 0 ; y < 4 ; y++){
					for(int z = 0 ; z < 4 ; z++){
						c.insert(s);
						s = we(s);
					}
					s=le(s);
				}
				s=no(s);
			}
		}
		cout << k << endl;
	}
}