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

struct cube {
	vector<string>v;
	int E = 2, S = 1, U = 0;
	void rotate(char C) {
		int a = E, b = S;
		switch (C) {
		case'E':E = U; U = 5 - a; break;
		case'S':S = U; U = 5 - b; break;
		case'L':E = S; S = 5 - a; break;
		}
	}
	bool operator==(cube b) {
		rep(i, 2){
			rep(j, 4) {
				rep(k, 4) {
					if (v[E] == b.v[b.E] && v[5 - E] == b.v[5 - b.E] && v[S] == b.v[b.S] && v[5 - S] == b.v[5 - b.S] && v[U] == b.v[b.U] && v[5 - U] == b.v[5 - b.U])return true;
					rotate('S');
				}
				rotate('E');
			}
			rotate('L');
		}
		return false;
	}
};
cube s[30];
bool used[30];
int main() {
	int n;
	while (cin >> n, n) {
		rep(i, n) {
			s[i].v = vector<string>(6);
			rep(j, 6)cin >> s[i].v[j];
		}
		vector<int>v[30];
		rep(i, n)for (int j = i + 1; j < n; j++) {
			if (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);
	}
}