#include <cstdio>
#include <string>

using namespace std;

int rot[6][4] = {{2, 4, 3, 1}, {2, 0, 3, 5}, {5, 4, 0, 1}, {0, 4, 5, 1}, {2, 5, 3, 0}, {3, 4, 2, 1}};

string board[30][6];

bool same(int a, int b)
{
	for (int i = 0; i < 6; i++){
		for (int j = 0; j < 6; j++){
			if (board[a][i] != board[b][j]) continue;
			for (int dx = 0; dx < 4; dx++){
				bool same = true;
				for (int k = 0; k < 4; k++){
					if (board[a][rot[i][(k + dx) % 4]] != board[b][rot[j][k]])
						same = false;
				}
				if (same) return (true);
			}
		}
	}
	return (false);
}

int main()
{
	int n;
	while (scanf("%d", &n) && n){
		int ct = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < 6; j++){
				char tmp[64];
				scanf("%s", tmp);
				board[i][j] = tmp;
			}
			bool ok = true;
			for (int j = 0; j < i; j++) if (same(i, j)) ok = false;
			ct += ok;
		}
		
		printf("%d\n", n - ct);
	}	
}