#include<iostream>
#include<string>
#include<vector>
using namespace std;

void shift(vector<char> &box, int set[4]){
	for(int i = 0;i < 3;i++){
		swap(box[set[i]], box[set[i + 1]]);
	}
}

int main(){
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		vector<char> box[30];
		for(int i = 0;i < n;i++){
			box[i].resize(6);
			for(int j = 0;j < 6;j++){
				string in;
				cin >> in;
				box[i][j] = in[0];
			}
		}
		int shiftSet[3][4] = {
			{1, 2, 4, 3},
			{0, 2, 5, 3},
			{0, 1 ,5, 4}
		};
		int ans = 0;
		for(int i = 0;i < n;i++){
			for(int j = i + 1;j < n;j++){
				vector<char> buf = box[j];
				//相手
				for(int x = 0;x < 4;x++){
					for(int y = 0;y < 4;y++){
						for(int z = 0;z < 4;z++){
							if(box[i] == buf){
								ans++;
								goto END;
							}
							shift(buf, shiftSet[0]);
						}
						shift(buf, shiftSet[1]);
					}
					shift(buf, shiftSet[2]);
				}
			}
END:;
		}
		cout << ans << endl;
	}
	return 0;
}