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

cube s[30];
bool used[30];
bool equal(cube a, cube b) {
	rep(i, 6) {
		if (a[i] != b[i])return false;
	}
	return true;
}
bool judge(cube a, cube b) {
	cube c;
	rep(i, 6) {
		c = a; a[3] = c[0]; a[0] = c[2]; a[2] = c[5]; a[5] = c[3];
		rep(j, 4) {
			c = a; a[3] = c[1]; a[1] = c[2]; a[2] = c[4]; a[4] = c[3];
			if (equal(a, b))return true;
		}
	}
	return false;
}
int main() {
	int n;
	while (cin >> n, n) {
		rep(i, n) {
			s[i] = cube(6);
			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);
	}
}