#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

string s[30][6];
bool used[30];
bool equal(string*a, string*b) {
	rep(i, 6) {
		if (a[i] != b[i])return false;
	}
	return true;
}
bool rotate(string*a, string*b) {
	string s[6];
	rep(j, 3) {
		rep(i, 6)s[i] = a[i];
		a[4] = s[0]; a[0] = s[1]; a[1] = s[4];
		rep(i, 4) {
			rep(k, 6)s[k] = a[k];
			a[3] = s[0]; a[0] = s[2]; a[2] = s[5]; a[5] = s[3];
			if (equal(a, b))return true;
		}
	}
	return false;
}
bool judge(string*a, string*b) {
	string s[6];
	if (rotate(a, b))return true;
	rep(i, 6)s[i] = a[i];
	a[3] = s[4]; a[2] = s[1]; a[1] = s[3]; a[4] = s[2];
	if (rotate(a, b))return true;
	rep(i, 6)s[i] = a[i];
	a[3] = s[1]; a[0] = s[2]; a[2] = s[4]; a[5] = s[3]; a[1] = s[5]; a[0] = s[4];
	if (rotate(a, b))return true;
	return false;
}
int main() {
	int n;
	while (cin >> n, n) {
		rep(i, n)rep(j, 6)cin >> s[i][j];
		vector<int>v[30];
		rep(i, n)for (int j = i + 1; j < n; j++) {
			if (judge(s[i], s[j])) {
				v[i].push_back(j);
			}
		}
		int cnt = 0;
		memset(used, 0, sizeof(used));
		rep(i, n) {
			if (used[i])continue;
			cnt++;
			for (int j : v[i])used[j] = true;
		}
		printf("%d\n", n - cnt);
	}
}