#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const double EPS  = 1e-9;

struct Dice {
	int num[6]; // top, bottom, front, back, right, left;
	static const int rd[3][4]; //?????¢??????
	
	Dice(int top, int bottom, int front, int back, int right, int left) {
		num[0] = top;		num[1] = bottom;
		num[2] = front;	num[3] = back;
		num[4] = right;	num[5] = left;
	}
	Dice(const Dice &d) {
		copy(d.num, d.num + 6, num);
	}
	
	bool operator < (const Dice &d) const {
		REP(i, 6) if (num[i] != d.num[i]) {
			return num[i] < d.num[i];
		}
		return false;
	}
	
	void X() { rotate(0); }
	void Y() { rotate(1); }
	void Z() { rotate(2); }
	
	void rotate(int p) {
		int tmp = num[rd[p][0]];
		
		REP(i, 3) num[rd[p][i]] = num[rd[p][i + 1]];
		num[rd[p][3]] = tmp;
	}
	
	Dice unique() {
		Dice now = *this;
		
		Dice mi(*this);
		
		REP(i, 4) {
			REP(j, 4) {
				REP(k, 4) {
					if (now < mi) mi = now;
					now.X();
				}
				now.Y();
			}
			now.Z();
		}
		return mi;
	}
	
	void print() {
		REP(i, 6) printf("%d%c", num[i], i == 5 ? '\n' : ' ');
	}
};

const int Dice::rd[3][4] = {
	{2,1,3,0}, // x
	{2,4,3,5}, // y
	{0,5,1,4}, // z
};

int main() {
	char color[6][10] = { "Red", "Yellow", "Blue", "Magenta", "Green", "Cyan" };
	map<string, int> trans;
	REP(i, 6) trans[color[i]] = i;
	
	int n;
	while (cin >> n, n) {
		set<Dice> ss;
		
		REP(i, n) {
			int col[6] = {};
			REP(j, 6) {
				string str; cin >> str;
				col[j] = trans[str];
			}
			
			ss.insert(Dice(col[0], col[5], col[1], col[4], col[2], col[3]).unique());
		}
		
		cout << n - ss.size() << endl;
	}
	
	return 0;
}