#ifndef ___Rank_Union_Find
#define ___Rank_Union_Find

#include <vector>

// ------ Class ------ //
class UnionFind {
private:
	unsigned size_; std::vector<unsigned> par, rank;
public:
	UnionFind() : size_(0), par(std::vector<unsigned>()), rank(std::vector<unsigned>()) {};
	UnionFind(unsigned size__) : size_(size__) {
		par.resize(size_); rank.resize(size_);
		for (unsigned i = 0; i < size_; i++) par[i] = i, rank[i] = 0;
	}
	unsigned size() { return size_; }
	unsigned root(unsigned x) { return par[x] == x ? x : par[x] = root(par[x]); }
	bool same(unsigned x, unsigned y) { return root(x) == root(y); }
	void unite(unsigned x, unsigned y) {
		x = root(x), y = root(y);
		if (x == y) return;
		if (rank[x] < rank[y]) par[x] = y;
		else if (rank[x] == rank[y]) par[y] = x, rank[x]++;
		else par[y] = x;
	}
	bool operator==(const UnionFind &u) { return par == u.par; }
	bool operator!=(const UnionFind &u) { return par != u.par; }
};

#endif

#include <set>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<int> x, y; int t;
bool same(vector<int> a, vector<int> b) {
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			for (int k = 0; k < 4; k++) {
				for (int l = 0; l < 4; l++) {
					string s;
					for (int m = 0; m < i; m++) s += 'N';
					for (int m = 0; m < j; m++) s += 'E';
					for (int m = 0; m < k; m++) s += 'S';
					for (int m = 0; m < l; m++) s += 'W';
					for (int i = 0; i < s.size(); i++) {
						t = a[0];
						switch (s[i]) {
						case 'N':
							a[0] = a[1];
							a[1] = a[5];
							a[5] = a[4];
							a[4] = t;
							break;
						case 'E':
							a[0] = a[3];
							a[3] = a[5];
							a[5] = a[2];
							a[2] = t;
							break;
						case 'S':
							a[0] = a[4];
							a[4] = a[5];
							a[5] = a[1];
							a[1] = t;
							break;
						case 'W':
							a[0] = a[2];
							a[2] = a[5];
							a[5] = a[3];
							a[3] = t;
							break;
						}
					}
					if (a == b) return true;
				}
			}
		}
	}
	return false;
}
int n; string s;
int main() {
	while (cin >> n, n) {
		vector<vector<int> > v(n, vector<int>(6));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 6; j++) {
				cin >> s;
				v[i][j] = s[0];
			}
		}
		UnionFind uf(n);
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (same(v[i], v[j])) uf.unite(i, j);
			}
		}
		set<int> s_;
		for (int i = 0; i < n; i++) s_.insert(uf.root(i));
		cout << n - s_.size() << endl;
	}
	return 0;
}